What is the purpose of Node.js module.exports and how do you use it?
I can't seem to find any information on this, but it appears to be a rather important part of Node.js as I often see it in source code.
According to the Node.js documentation:
module
A reference to the current
module
. In particularmodule.exports
is the same as the exports object. Seesrc/node.js
for more information.
But this doesn't really help.
What exactly does module.exports
do, and what would a simple example be?
Some few things you must take care if you assign a reference to a new object to
exports
and /ormodules.exports
:1. All properties/methods previously attached to the original
exports
ormodule.exports
are of course lost because the exported object will now reference another new oneThis one is obvious, but if you add an exported method at the beginning of an existing module, be sure the native exported object is not referencing another object at the end
2. In case one of
exports
ormodule.exports
reference a new value, they don't reference to the same object any more3. Tricky consequence. If you change the reference to both
exports
andmodule.exports
, hard to say which API is exposed (it looks likemodule.exports
wins)Note that the NodeJS module mechanism is based on CommonJS modules which are supported in many other implementations like RequireJS, but also SproutCore, CouchDB, Wakanda, OrientDB, ArangoDB, RingoJS, TeaJS, SilkJS, curl.js, or even Adobe Photoshop (via PSLib). You can find the full list of known implementations here.
Unless your module use node specific features or module, I highly encourage you then using
exports
instead ofmodule.exports
which is not part of the CommonJS standard, and then mostly not supported by other implementations.Another NodeJS specific feature is when you assign a reference to a new object to
exports
instead of just adding properties and methods to it like in the last example provided by Jed Watson in this thread. I would personally discourage this practice as this breaks the circular reference support of the CommonJS modules mechanism. It is then not supported by all implementations and Jed example should then be written this way (or a similar one) to provide a more universal module:(sayhello.js):
(app.js):
Or using ES6 features
(sayhello.js):
(app.js):
PS: It looks like Appcelerator also implements CommonJS modules, but without the circular reference support (see: Appcelerator and CommonJS modules (caching and circular references))
The intent is:
Wikipedia
I imagine it becomes difficult to write a large programs without modular / reusable code. In nodejs we can create modular programs utilising
module.exports
defining what we expose and compose our program withrequire
.fileLog.js
stdoutLog.js
program.js
execute
Now try swapping ./stdoutLog.js for ./fileLog.js.
This has already been answered but I wanted to add some clarification...
You can use both
exports
andmodule.exports
to import code into your application like this:var mycode = require('./path/to/mycode');
The basic use case you'll see (e.g. in ExpressJS example code) is that you set properties on the
exports
object in a .js file that you then import usingrequire()
So in a simple counting example, you could have:
(counter.js):
... then in your application (web.js, or really any other .js file):
In simple terms, you can think of required files as functions that return a single object, and you can add properties (strings, numbers, arrays, functions, anything) to the object that's returned by setting them on
exports
.Sometimes you'll want the object returned from a
require()
call to be a function you can call, rather than just an object with properties. In that case you need to also setmodule.exports
, like this:(sayhello.js):
(app.js):
The difference between exports and module.exports is explained better in this answer here.
When dividing your program code over multiple files,
module.exports
is used to publish variables and functions to the consumer of a module. Therequire()
call in your source file is replaced with correspondingmodule.exports
loaded from the module.Remember when writing modules
module.exports
object is also available asexports
shorthand. But when returning a sole function, always usemodule.exports
.According to: "Modules Part 2 - Writing modules".
the module.exports property or the exports object allows a module to select what should be shared with the application
I have a video on module_export available here