I\'ve found the following contract in a Node.js module:
module.exports = exports = nano = function database_module(cfg) {...}
I wonder what
module.exports
and exports
both point to the same object before the module is evaluated.
Any property you add to the module.exports
object will be available when your module is used in another module using require
statement. exports
is a shortcut made available for the same thing. For instance:
module.exports.add = (a, b) => a+b
is equivalent to writing:
exports.add = (a, b) => a+b
So it is okay as long as you do not assign a new value to exports
variable. When you do something like this:
exports = (a, b) => a+b
as you are assigning a new value to exports
it no longer has reference to the exported object and thus will remain local to your module.
If you are planning to assign a new value to module.exports
rather than adding new properties to the initial object made available, you should probably consider doing as given below:
module.exports = exports = (a, b) => a+b
Node.js website has a very good explanation of this.