module.exports vs exports in Node.js

前端 未结 23 1627
自闭症患者
自闭症患者 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:02

    Here is the result of

    console.log("module:");
    console.log(module);
    
    console.log("exports:");
    console.log(exports);
    
    console.log("module.exports:");
    console.log(module.exports);
    

    Also:

    if(module.exports === exports){
        console.log("YES");
    }else{
        console.log("NO");
    }
    
    //YES
    

    Note: The CommonJS specification only allows the use of the exports variable to expose public members. Therefore, the named exports pattern is the only one that is really compatible with the CommonJS specification. The use of module.exports is an extension provided by Node.js to support a broader range of module definition patterns.

提交回复
热议问题