What does the module object do?
1. What module is
moduleis a built-in Node.js object that represents the current module (the current file).Every
.jsfile in Node.js is a separate module, and Node automatically creates amoduleobject for each one.
Example:
console.log(module);Prints (simplified):
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.exportsis the object that defines what actually gets returned when somethingrequire()s this file.
Whatever you assign to module.exports
becomes the result of calling require().
math.js
const PI = 3.14;
function add(a, b) {
return a + b;
}
// export it
module.exports = { PI, add };app.js
const math = require('./math');
console.log(math.add(2, 3)); // 5
console.log(math.PI); // 3.14In other words:
require('./math')returns the value ofmodule.exportsfrommath.js.
3. How module and exports relate
Node.js wraps every file in an internal function:
(function (exports, require, module, __filename, __dirname) {
// your code
});Here:
exportsis a reference (an alias) tomodule.exports;module.exportsis the actual object that gets exported.
Schematically:
exports ──► { } ◄── module.exportsAs long as you write:
exports.foo = 42;or
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:
// math.js
exports = { add: (a, b) => a + b };app.js
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:
module.exports = {
add: (a, b) => a + b
};Option 2: via exports:
exports.add = (a, b) => a + b;
exports.sub = (a, b) => a - b;Option 3: exporting a function directly:
module.exports = function (a, b) {
return a + b;
};5. require() and module.exports under the hood
When you call:
const user = require('./user');Node.js does this:
- Loads
user.js; - Creates an object:
const module = { exports: {} };
const exports = module.exports;- Runs the file's code;
- Returns
module.exports.
6. A full-cycle example
user.js
console.log('Loading the user module...');
module.exports = {
name: 'Tim',
sayHi() {
console.log('Hi!');
}
};app.js
const user = require('./user');
console.log(user.name); // Tim
user.sayHi(); // Hi!Output:
Loading the user module...
Tim
Hi!7. Common mistakes
| Mistake | Why | Fix |
|---|---|---|
exports = {...} | The link to module.exports is lost | Use module.exports = {...} |
| Exporting inside a function | It doesn't reach outside the function | Define module.exports at the top level |
| Several exports in a row | The last one overwrites the previous ones | Export one object with every property |
8. An example: adding vs reassigning
Adding:
exports.a = 1;
exports.b = 2;Result:
{ a: 1, b: 2 }Reassigning:
module.exports = { a: 1, b: 2 };Result:
{ a: 1, b: 2 }Both work, but exports = ... without module.exports = ... doesn't!
9. The module object's metadata
| Property | Purpose |
|---|---|
id | The module's unique identifier |
filename | The absolute path to the file |
loaded | A flag for whether the module fully loaded |
parent | The module that called require() |
children | Modules loaded by this module |
exports | The object returned by require() |
path | The folder the module is in |
10. Visually
┌────────────────────────────┐
│ module │
│ ┌──────────────────────┐ │
│ │ module.exports = {} │◄─┼─── returned by require()
│ └──────────────────────┘ │
│ exports ─────► (a reference)│
└────────────────────────────┘Quick summary
| Object | Purpose |
|---|---|
module | A service object representing the current module file |
module.exports | The object that actually gets exported |
exports | A 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 readyA concise answer to help you respond confidently on this topic during an interview.