The module object
History
ObjectIn each module, the module free variable is a reference to the object
representing the current module. For convenience, module.exports is
also accessible via the exports module-global. module is not actually
a global but rather local to each module.
module[]The module objects required for the first time by this one.
ObjectThe module.exports object is created by the Module system. Sometimes this is
not acceptable; many want their module to be an instance of some class. To do
this, assign the desired export object to module.exports. Assigning
the desired object to exports will simply rebind the local exports variable,
which is probably not what is desired.
For example, suppose we were making a module called a.js:
const EventEmitter = require('node:events'); module.exports = new EventEmitter(); // Do some work, and after some time emit // the 'ready' event from the module itself. setTimeout(() => { module.exports.emit('ready'); }, 1000);
Then in another file we could do:
const a = require('./a'); a.on('ready', () => { console.log('module "a" is ready'); });
Assignment to module.exports must be done immediately. It cannot be
done in any callbacks. This does not work:
x.js:
setTimeout(() => { module.exports = { a: 'hello' }; }, 0);
y.js:
const x = require('./x'); console.log(x.a);
exports shortcut
History
The exports variable is available within a module's file-level scope, and is
assigned the value of module.exports before the module is evaluated.
It allows a shortcut, so that module.exports.f = ... can be written more
succinctly as exports.f = .... However, be aware that like any variable, if a
new value is assigned to exports, it is no longer bound to module.exports:
module.exports.hello = true; // Exported from require of module exports = { hello: false }; // Not exported, only available in the module
When the module.exports property is being completely replaced by a new
object, it is common to also reassign exports:
module.exports = exports = function Constructor() { // ... etc. };
To illustrate the behavior, imagine this hypothetical implementation of
require(), which is quite similar to what is actually done by require():
function require(/* ... */) { const module = { exports: {} }; ((module, exports) => { // Module code here. In this example, define a function. function someFunc() {} exports = someFunc; // At this point, exports is no longer a shortcut to module.exports, and // this module will still export an empty default object. module.exports = someFunc; // At this point, the module will now export someFunc, instead of the // default object. })(module, module.exports); return module.exports; }
stringThe fully resolved filename of the module.
stringThe identifier for the module. Typically this is the fully resolved filename.
booleantrue if the module is running during the Node.js preload
phase.booleanWhether or not the module is done loading, or is in the process of loading.
require.main and
module.children instead.The module that first required this one, or null if the current module is the
entry point of the current process, or undefined if the module was loaded by
something that is not a CommonJS module (E.G.: REPL or import).
stringThe directory name of the module. This is usually the same as the
path.dirname() of the module.id.
string[]The search paths for the module.
module.require(id): any
The module.require() method provides a way to load a module as if
require() was called from the original module.
In order to do this, it is necessary to get a reference to the module object.
Since require() returns the module.exports, and the module is typically
only available within a specific module's code, it must be explicitly exported
in order to be used.