Node.js Reference ยท Globals and Console
Node.js module and exports
Reference for the CommonJS module object in Node.js: module.exports vs exports, module.id, filename, loaded, children, paths and common export mistakes.
In a CommonJS file, module is an object that represents the file itself, and module.exports is the value other files receive from require(). exports is a shortcut variable that starts out pointing at the same object as module.exports.
What require() returns is always module.exports, never exports. That one rule explains almost every export bug.
// cjs module.exports = { add: (a, b) => a + b }; exports.sub = (a, b) => a - b; // lost: exports no longer points at module.exports
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
module.exports = value; exports.name = value;
| Property | Description |
|---|---|
module.exports |
The value require() returns. Starts as {}. Assign any value: object, function, class, number. |
exports |
A variable holding the initial module.exports object. Adding properties works; reassigning it does nothing for importers. |
module.id |
'.' for the entry file, otherwise the absolute filename. |
module.filename |
Absolute path of the file. |
module.loaded |
false while the file runs, true after it has finished loading. |
module.children |
Modules this file required for the first time. |
module.paths |
The node_modules folders searched for packages. |
module.parent |
Deprecated; use require.main or module.children. |
Edge cases: export assignments are not hoisted, so code that sets module.exports after an early require cycle can leave the other side with the old empty object.
Usage notes
- Pick one style per file. Either add properties to
exports, or assignmodule.exportsonce at the end. Mixing the two is the classic way to lose exports. - Export a function or class with
module.exports = ...; useexports.name = ...for a bag of helpers. thisat the top level of a CommonJS file equalsmodule.exports, which is whythis.x = 1also exportsx. Avoid relying on it.- Exports are live objects, not copies, but primitive values are copied at
requiretime. If you export a counter number and change it later, importers keep the old value; export a getter or function instead. - Interop: an ES module importing a CommonJS file gets
module.exportsas the default export, plus named exports Node can detect statically. See the interop lesson for the details. - ES modules have no
module. Useexportandexport defaultinstead;moduleis not defined there. - Set
module.exportsbefore circular requires run, or the other file receives an incomplete object.