I've found the following contract in a Node.js module:
module.exports = exports = nano = function database_module(cfg) {...}
I wonder what's the difference between module.exports
and exports
and why both are used here.
Module exports are the instructions that tell Node. js which bits of code (functions, objects, strings, etc.) to export from a given file so that other files are allowed to access the exported code.
exports we can separate business logic from other modules. In other terms, we can achieve abstraction using it. By Using it becomes easy to maintain and manage the code base in different modules. Enforces separation of concerns.
Module exports are the instruction that tells Node. js which bits of code (functions, objects, strings, etc.) to “export” from a given file so other files are allowed to access the exported code.
If you categories exports as 'export of domestic goods' and export of 'foreign goods', the difference between export and re export makes easier to follow. In simple terms, exports mean export of domestic goods moved out to a foreign country.
Even though question has been answered and accepted long ago, i just want to share my 2 cents:
You can imagine that at the very beginning of your file there is something like (just for explanation):
var module = new Module(...); var exports = module.exports;
So whatever you do just keep in mind that module.exports
and NOT exports
will be returned from your module when you're requiring that module from somewhere else.
So when you do something like:
exports.a = function() { console.log("a"); } exports.b = function() { console.log("b"); }
You are adding 2 functions a
and b
to the object to which module.exports
points, so the typeof
the returning result will be an object
: { a: [Function], b: [Function] }
Of course, this is the same result you will get if you are using module.exports
in this example instead of exports
.
This is the case where you want your module.exports
to behave like a container of exported values. Whereas, if you only want to export a constructor function then there is something you should know about using module.exports
or exports
;(Remember again that module.exports
will be returned when you require something, not export
).
module.exports = function Something() { console.log('bla bla'); }
Now typeof
returning result is 'function'
and you can require it and immediately invoke like: var x = require('./file1.js')();
because you overwrite the returning result to be a function.
However, using exports
you can't use something like:
exports = function Something() { console.log('bla bla'); } var x = require('./file1.js')(); //Error: require is not a function
Because with exports
, the reference doesn't point anymore to the object where module.exports
points, so there is not a relationship between exports
and module.exports
anymore. In this case module.exports
still points to the empty object {}
which will be returned.
The accepted answer from another topic should also help: Does JavaScript pass by reference?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With