- Module.exports and exports are the same objects referenced to, similar to the following code (for example, not completely correct):
var module.exports = {};
var exports = Module.exports;
Within the module, they point to the same empty object before any changes are made to module.exports and exports.
- The use of require in other modules refers to the Module.exports object, which is not necessarily the reference object that the exports points to, which can easily be caused by ignoring the bug in development. Here's an example:
Exports = function () {};
Within the other modules
Require (...); /get {}
When using require in other modules to refer to an object exported by the above module, it is found to be an empty object, why? Because Exports=function () {} is used, the reference to exports is pointed to the function, and the Module.exports reference is an empty object, and its His module uses require to refer to module.exports, so there is an empty object, not a function. Of course this is written module.exports = function () {} is definitely not a problem.
- Some people say that in order to prevent the above bug to occur, it is generally performed under exports = Module.exports, but the direct overwrite module.exports is not a good habit, so for module.exports = {a:123, b:456} Such a situation, separate to write:
EXPORTS.A = 123;
exports.b = 456;
- Here are a few cases where other modules get different results:
Exports = function fn () {}; Outputs "@routes {}"
Exports.fn = function fn () {}; Outputs "@routes {fn: [FUNCTION:FN]}"
Module.exports = function fn () {}; Outputs "@routes function fn () {}"
Module.exports.fn = function fn () {}; Outputs "@routes {fn: [FUNCTION:FN]}"
Module.export and Export