module.exports vs exports in Node.js

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

    JavaScript passes objects by copy of a reference

    It's a subtle difference to do with the way objects are passed by reference in JavaScript.

    exports and module.exports both point to the same object. exports is a variable and module.exports is an attribute of the module object.

    Say I write something like this:

    exports = {a:1};
    module.exports = {b:12};
    

    exports and module.exports now point to different objects. Modifying exports no longer modifies module.exports.

    When the import function inspects module.exports it gets {b:12}

提交回复
热议问题