Home > Article > Web Front-end > What are the two ways to export modules in node?
Two ways for node to export modules: 1. Using exports, this method can be exported by adding attributes, and multiple members can be exported; 2. Using "module.exports", this method can be exported directly through When exporting a module by assigning a value to "module.exports", only a single member can be exported.
The operating environment of this tutorial: windows10 system, nodejs version 12.19.0, Dell G3 computer.
There are two ways to export a module.
exports can export multiple members
module.exports can only export a single member, and the later ones will overwrite the previous
##1, module.exports
We can export the module directly by assigning values to module.exportsmodule.exports can export a single memberThe functions of exports can be realized by module.exports2. exports
The exports variable is a reference to module.exports provided by node. exports is exported by adding attributes, and only one object can be exported. If you directly point the exports variable to a value, it will not affect module.exports, but this is equivalent to cutting off the connection between exports and module.exports.Examples are as follows:
// 定义方法,常量 const myPI = 3.14 const add = (a,b) => a + b; // 导出,两种方法任意都可以 // 方法一: exports.myPI = myPI exports.add = add // 方法二: module.exports.myPI = myPI module.exports.add = add // 方法二(变形) module.exports = { myPI, add }Recommended learning: "
nodejs video tutorial"
The above is the detailed content of What are the two ways to export modules in node?. For more information, please follow other related articles on the PHP Chinese website!