module.exports vs exports in Node.js

前端 未结 23 1276
自闭症患者
自闭症患者 2020-11-22 06:11

I\'ve found the following contract in a Node.js module:

module.exports = exports = nano = function database_module(cfg) {...}

I wonder what

23条回答
  •  悲哀的现实
    2020-11-22 06:56

    I just make some test, it turns out that, inside nodejs's module code, it should something like this:

    var module.exports = {};
    var exports = module.exports;
    

    so:

    1:

    exports = function(){}; // this will not work! as it make the exports to some other pointer
    module.exports = function(){}; // it works! cause finally nodejs make the module.exports to export.
    

    2:

    exports.abc = function(){}; // works!
    exports.efg = function(){}; // works!
    

    3: but, while in this case

    module.exports = function(){}; // from now on we have to using module.exports to attach more stuff to exports.
    module.exports.a = 'value a'; // works
    exports.b = 'value b'; // the b will nerver be seen cause of the first line of code we have do it before (or later)
    

提交回复
热议问题