module.exports vs exports in Node.js

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

    Let's create one module with 2 ways:

    One way

    var aa = {
        a: () => {return 'a'},
        b: () => {return 'b'}
    }
    
    module.exports = aa;
    

    Second way

    exports.a = () => {return 'a';}
    exports.b = () => {return 'b';}
    

    And this is how require() will integrate module.

    First way:

    function require(){
        module.exports = {};
        var exports = module.exports;
    
        var aa = {
            a: () => {return 'a'},
            b: () => {return 'b'}
        }
        module.exports = aa;
    
        return module.exports;
    }
    

    Second way

    function require(){
        module.exports = {};
        var exports = module.exports;
    
        exports.a = () => {return 'a';}
        exports.b = () => {return 'b';}
    
        return module.exports;
    }
    

提交回复
热议问题