What is the scope of require module in Node.js ?
Last Updated :
24 Apr, 2025
Whenever we have to use any feature of any other module shared by “module.exports” in our Node.js application, then we use require() method. That means we are importing those shared features into our own module and after that, we can use those features.
Now the biggest question is -what is the scope of that required module? So if we want a clear answer then it has a module scope in which it is required. This means any module imported by require function/method in a module (a javascript file) is accessible in that entire module. But if that is required inside any function of that module then it will have that function’s scope.
The require() function takes the module’s file path in the case of a user-defined module and takes the module name in the case of a third-party module. Now, whenever we try to require any module then NodeJS does the following five things:
- Resolving and Loading: Here module’s absolute path is traced and then it will be loaded. If no module is found then it will throw an error
- Wrapping: After loading of node modules it gets wrapped in a function so that it gets the private scope
- Evaluation: Here code inside that wrapper function is executed.
- Returning exports: Then whatever is present in that got returned by require()
- Caching: All the required modules got cached so whenever it will be required again then there is no need to get again by this process.
Now let us see “How the scope of any required module works?” in the case of user-defined modules and third-party module
So first we will make two files that are “app.js” and “module.js”. “app.js” will be used to import any module and “module.js” is used for making user-defined modules.
.png)
Project Structure
User-defined Module: In this case, we will design a module by ourselves and we will export one of the functions from that module and import that using “require” into another module then that can be accessed entirely in that module.
Steps for using a user-defined module:
- create different functions into module.js
- Export any one function or all the functions from “module.js”
- Import all the things that are exported by “module.js” via the “require” method
- Use all the things to get the desired result
Example 1: In this example, we created a function in “module.js” that prints “GeeksforGeeks” in the console and exported that. Then later we imported that into our “app.js”
module.js
Javascript
function printGFG() {
console.log( "GeeksforGeeks" );
}
module.exports = printGFG;
|
app.js
Javascript
const data = require( "./module.js" );
data();
|
To run the application, write the following in your terminal after going to the location of app.js and module.js
node app.js
If you have installed “nodemon” globally then you can also write
nodemon app.js
Output:
GeeksforGeeks
Example 2: Here we will be using the “express” module. So first install that into our node application
npm install express
app.js
Javascript
const express = require( "express" );
const app = express();
app.get( "/" , (req, res) => {
res.send( "<h1>GeeksforGeeks</h1>" );
});
app.listen(3000, (err) => {
if (err) {
console.log( "Some Error Occurred" );
console.log(err);
} else {
console.log( "Server Started on a port 3000" );
}
});
|
Now after writing “node app.js” in the terminal, we can get the following output at “localhost:3000”
Output:
.png)
What’s the scope of the required (require()) modules in Node.js?
So we can clearly see that once any module gets required in any module(javascript file), then it can be accessed entirely in that module(javascript file).
Similar Reads
What is the purpose of module.exports in node.js ?
The module.exports is actually a property of the module object in node.js. module. Exports is the object that is returned to the require() call. By module.exports, we can export functions, objects, and their references from one file and can use them in other files by importing them by require() meth
3 min read
What is the purpose of the 'node_modules' folder ?
The node_modules folder is a directory in NodeJS projects that stores third-party libraries and dependencies. It's essential for managing dependencies, which are packages or modules that a NodeJS project relies on. When you install a package using npm or Yarn, these tools download the package along
5 min read
What is the purpose of process object in Node.js ?
A process object is a global object available in the Node.js environment. It is globally available. We do not have to use the require() to import the module of the process object. The "process" object use to get current Node.js process details & also give control over that process. Properties of
2 min read
What is the purpose of the process object in Node JS ?
In NodeJS, the process object is a global object that provides access to information and control over the current NodeJS process. It offers various properties and methods to interact with the underlying operating system environment and manage the NodeJS process effectively. Let's explore the primary
2 min read
What is the role of assert in Node.js ?
Assert is a Node.js module that provides facilitates to writing the test and will not provide any output on the terminal when the test is in the process until any asserting error during the test. It provides various set assertion functions that can be used verifying constants. The assert module in m
2 min read
What is File System Module in Node.js ?
Node.js is a powerful platform for building server-side applications, and one of its key features is its ability to interact with the file system. The File System (fs) module in Node.js provides an extensive API to work with files and directories, allowing developers to perform operations such as re
3 min read
Node.js require Module
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
3 min read
Node.js Request Module
The request module in Node is used to make HTTP requests. It allows developers to simply make HTTP requests like GET, POST, PUT, and DELETE in the Node app. It follows redirects by default. Syntax:const request = require('request'); request(url, (error, response, body) => { if (!error &&
2 min read
What is the Purpose of __filename Variable in Node.js ?
In Node.js, there are several global variables that are available in all modules. One such variable is __filename. This article delves into the purpose, usage, and practical applications of the __filename variable in Node.js. What is __filename?The __filename variable is a built-in global variable i
3 min read
What is Local, Script and Global Scope in Node.js ?
In Node.js, scope determines the accessibility or visibility of variables, functions, and objects in some particular part of your code. The word scope means the extent to which we can use something. In programming variable scope means the extent to which we can use a variable within a program. In Ja
3 min read