Suggest an editImprove this articleRefine the answer for “What does the module object do?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)`module` is a service object representing the current module file, holding metadata (`id`, `filename`, `exports`); `module.exports` is what actually gets returned when the file is `require()`'d, and `exports` is just a convenient reference to it. **Key point:** reassigning `exports = {...}` breaks the link to `module.exports`, so the file ends up exporting an empty object - always change `module.exports` itself.Shown above the full answer for quick recall.Answer (EN)Image## 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; ``` 3. Runs the file's code; 4. 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 | 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: ```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 | 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 ```javascript ┌────────────────────────────┐ │ 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.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.