assigning “exports” to a function in nodejs module doesn't work

后端 未结 2 1630
暗喜
暗喜 2021-01-29 05:04

in one file (otherFile.js) I have this:

exports = function() {}

in my main file I have this:

var thing = require(\'./otherFile.         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-29 05:54

    Consider this code:

    !function(exports) {
        // your module code
        exports = 12345
    }(module.exports)
    
    console.log(module.exports)
    

    It won't work, and it's easy to see why. Node.js exports object is defined exactly like this, with an exception of arguments ordering.


    So always use module.exports instead of exports, it'll do the right thing.

提交回复
热议问题