Require is used to load code, while exports and module.exports are used to export code, from the Touch of node. js, they are not two strangers, on the code:
Foo.js
function () { console.log (' a '
Test.js
var x = require ('./foo '); Console.log (x.a)//1
Foo.js
function () { console.log (' a '= {a:2
Test.js
var x = require ('./foo '); Console.log (X.A)//2
All the properties and methods collected by the exports are assigned to Module.exports. Of course, there is a premise that the module.exports itself does not have any attributes or methods. If Module.exports already has some properties and methods, the information collected by exports will be ignored.
Can understand this, Each node. js execution file automatically creates a module object, and the module object creates a exports property, initializes the value to an empty object, exports references the exports property of the module object, and when Module.exports is changed Time, exports will not change. When the module is exported, the actual execution is module.exports.
The difference between node. JS Module.exports and Exports