Skip to main content

What does the module object do?

1. What module is

module is a built-in Node.js object that represents the current module (the current file).

Every .js file in Node.js is a separate module, and Node automatically creates a module object for each one.

Example:

javascript
console.log(module);

Prints (simplified):

javascript
Module { id: '.', path: '/Users/tim/project', exports: {}, filename: '/Users/tim/project/app.js', loaded: false, children: [], parent: null }

It's a "service object" holding metadata:

  • id, the module's unique identifier (usually a path);
  • filename, the absolute path to the file;
  • loaded, whether the module has fully loaded;
  • exports, the object being exported;
  • children, the list of modules this module loaded;
  • parent, whoever loaded this module.

2. What module.exports does

module.exports is the object that defines what actually gets returned when something require()s this file.

Whatever you assign to module.exports becomes the result of calling require().

math.js

javascript
const PI = 3.14; function add(a, b) { return a + b; } // export it module.exports = { PI, add };

app.js

javascript
const math = require('./math'); console.log(math.add(2, 3)); // 5 console.log(math.PI); // 3.14

In other words:

require('./math') returns the value of module.exports from math.js.

3. How module and exports relate

Node.js wraps every file in an internal function:

javascript
(function (exports, require, module, __filename, __dirname) { // your code });

Here:

  • exports is a reference (an alias) to module.exports;
  • module.exports is the actual object that gets exported.

Schematically:

javascript
exports ──► { } ◄── module.exports

As long as you write:

javascript
exports.foo = 42;

or

javascript
module.exports.foo = 42;

it's the same thing (both add properties to the same shared object).

4. But there's a catch!

If you reassign exports directly, the link to module.exports is lost.

An example of incorrect usage:

javascript
// math.js exports = { add: (a, b) => a + b };

app.js

javascript
const math = require('./math'); console.log(math); // {}

Why? Because you just reassigned the local exports variable, while module.exports stayed an empty object.

The correct ways to export:

Option 1: via module.exports:

javascript
module.exports = { add: (a, b) => a + b };

Option 2: via exports:

javascript
exports.add = (a, b) => a + b; exports.sub = (a, b) => a - b;

Option 3: exporting a function directly:

javascript
module.exports = function (a, b) { return a + b; };

5. require() and module.exports under the hood

When you call:

javascript
const user = require('./user');

Node.js does this:

  1. Loads user.js;
  2. Creates an object:
javascript
const module = { exports: {} }; const exports = module.exports;
  1. Runs the file's code;
  2. Returns module.exports.

6. A full-cycle example

user.js

javascript
console.log('Loading the user module...'); module.exports = { name: 'Tim', sayHi() { console.log('Hi!'); } };

app.js

javascript
const user = require('./user'); console.log(user.name); // Tim user.sayHi(); // Hi!

Output:

javascript
Loading the user module... Tim Hi!

7. Common mistakes

MistakeWhyFix
exports = {...}The link to module.exports is lostUse module.exports = {...}
Exporting inside a functionIt doesn't reach outside the functionDefine module.exports at the top level
Several exports in a rowThe last one overwrites the previous onesExport one object with every property

8. An example: adding vs reassigning

Adding:

javascript
exports.a = 1; exports.b = 2;

Result:

javascript
{ a: 1, b: 2 }

Reassigning:

javascript
module.exports = { a: 1, b: 2 };

Result:

javascript
{ a: 1, b: 2 }

Both work, but exports = ... without module.exports = ... doesn't!

9. The module object's metadata

PropertyPurpose
idThe module's unique identifier
filenameThe absolute path to the file
loadedA flag for whether the module fully loaded
parentThe module that called require()
childrenModules loaded by this module
exportsThe object returned by require()
pathThe folder the module is in

10. Visually

javascript
┌────────────────────────────┐ │ module │ │ ┌──────────────────────┐ │ │ │ module.exports = {} │◄─┼─── returned by require() │ └──────────────────────┘ │ │ exports ─────► (a reference)└────────────────────────────┘

Quick summary

ObjectPurpose
moduleA service object representing the current module file
module.exportsThe object that actually gets exported
exportsA reference to module.exports (a convenient shortcut)
require()Loads a module and returns module.exports

The core rule:

Anything you want to expose, put it on module.exports.

Everything else stays private inside the module.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.