The primary object exported by theĀ require()
Ā module is a function. When NodeJS invokes thisĀ require()
Ā function, it does so with a singular argument - the file path. This invocation triggers a sequence of five pivotal steps:
- Resolving and Loading: The process begins with the resolution and loading of the module.
- Wrapping: The module is then encapsulated within a function wrapper.
- Execution: The function wrapper is executed, bringing the module to life.
- Returning Exports: The moduleās exports are returned, making them accessible for use.
- Caching: Finally, the module is cached to optimize future requests.
Each of these steps plays a crucial role in the functioning of the module system, and weāll delve into each one in greater detail as we progress through this article.
Let's look at each step in more detail.
Resolving and Loading:
- In this step, Nodes decide which module to load core module or developer module or 3rd-party module using the following steps:
- When require function receive the module name as its input, It first tries to load core module.
- If path in require function begins with './' or '../' It will try to load developer module.
- If no file is find, it will try to find folder with index.js in it .
- Else it will go to node_modules/ and try to load module from here .
- If file is still not found , then an error is thrown .
Wrapping:
Once the module is loaded , the module code is wrapped in a special function which will give access to a couple of objects. Wrapping is done to give that loaded file a private scope or local scope. So that it can't be accessed globally.
Folder Structure:
Example 1:
module2.js
// Caching
const mod = require('./module1.js')
module1.js
console.log(require("module").wrapper);
Output:
[
'(function (exports, require, module, __filename, __dirname) { ',
'\n});'
]

- Execution: In this part, the code of the module or code inside the wrapper function run by the NodeJS runtime.
- Returning Exports: In this part, require function return the exports of required module. These exports are stored in module.exports.Use module.exports to export single variable/class/function. If you want to export multiple function or variables use exports Ā ( exports.add = (a,b)=>a+b ).
Returning Exports - Caching: At the end all modules are cached after the first time they are loaded e.g. If you require the same module multiple times, you will get the same result. So the code and modules are executed in the first call and in a subsequent call, results are retrieved from the cache.
Example 2: Lets take an example to understand caching
module1.js
console.log("Hello GEEKSFORGEEKS");
module.exports = ()=> console.log("GeeksForGeeks is the best !!");
module2.js
// Caching
const mod = require('./module1.js');
mod();
mod();
mod();
Output:
Hello GEEKSFORGEEKS
GeeksForGeeks is the best !!
GeeksForGeeks is the best !!
GeeksForGeeks is the best !!
What happen when we require() a Module in Node,js
Similar Reads
Node.js Utility Module The util module in Node.js provides a variety of utility functions that assist with tasks such as debugging, formatting, and inheritance. It includes methods for inspecting objects, formatting strings, and extending classes. Node.js Utility ModuleThe util module offers essential utilities that are n
4 min read
Node.js VM Module The VM (Virtual Machine) module in Node.js lets you safely run JavaScript code in a separate, isolated environment. This feature is especially handy when you need to execute code without affecting the rest of your application, making it ideal for handling untrusted code or creating distinct executio
4 min read
Node.js V8 Module The v8 module in Node.js is a core module that provides an interface to interact with the V8 JavaScript engine, which is the engine that Node.js uses to execute JavaScript code. This module exposes a variety of V8-specific APIs that allow developers to manage memory usage, optimize performance, and
5 min read
Node.js Path Module The path module in Node.js is a core module that offers utilities for managing file and directory paths. It helps handle and transform paths across different operating systems, ensuring platform independence. The module includes methods for joining, resolving, and normalizing paths, among other comm
5 min read
Node.js Local Module A local module in Node.js refers to a custom module created in an application. Unlike the built in or third-party modules, local modules are specific to the project and are used to organize and reuse your code across different parts of your application.Local Module in Node.jsLocal modules in Node.js
2 min read