module.exports vs exports in Node.js

前端 未结 23 1237
自闭症患者
自闭症患者 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 07:04

    Here is a good description written about node modules in node.js in action book from Manning publication.
    What ultimately gets exported in your application is module.exports.
    exports
    is set up simply as a global reference to module.exports , which initially is defined as an empty object that you can add properties to. So exports.myFunc is just shorthand for module.exports.myFunc.

    As a result, if exports is set to anything else, it breaks the reference between module.exports and exports . Because module.exports is what really gets exported, exports will no longer work as expected—it doesn’t reference module .exports anymore. If you want to maintain that link, you can make module.exports reference exports again as follows:

    module.exports = exports = db;
    

提交回复
热议问题