Skip to content
Tutorials Center
My learning

Topics

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.

NODE
// cjs
module.exports = { add: (a, b) => a + b };
exports.sub = (a, b) => a - b; // lost: exports no longer points at module.exports

Try it

Node.js exports is only a shortcut

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.

Node.js Inspecting the module object

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

NODE
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 assign module.exports once at the end. Mixing the two is the classic way to lose exports.
  • Export a function or class with module.exports = ...; use exports.name = ... for a bag of helpers.
  • this at the top level of a CommonJS file equals module.exports, which is why this.x = 1 also exports x. Avoid relying on it.
  • Exports are live objects, not copies, but primitive values are copied at require time. 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.exports as the default export, plus named exports Node can detect statically. See the interop lesson for the details.
  • ES modules have no module. Use export and export default instead; module is not defined there.
  • Set module.exports before circular requires run, or the other file receives an incomplete object.