What are Modules in Node.js ?
Last Updated :
18 Jan, 2023
In Node.js Application, a Module can be considered as a block of code that provide a simple or complex functionality that can communicate with external application. Modules can be organized in a single file or a collection of multiple files/folders. Almost all programmers prefer modules because of their reusability throughout the application and ability to reduce the complexity of code into smaller pieces.
Nodejs uses the CommonJS Module standard implementation in its module ecosystem.
Types of Modules: In Nodejs, there is 3 type of modules namely
- Core Modules
- Local Modules
- Third-Party Modules
Core Modules: Node.js comes with dozens of built-in modules. These built-in modules are sometimes referred to as core modules. The module system is built around the require function. This function is used to load a module and get access to its contents. require is a global variable provided to all your Node.js scripts, so you can use it anywhere you like. require() function will return a JavaScript type depending on your module.
Syntax for Importing Module:
const module = require("Name_of_Module_to_be_imported");
How to use Core Module: You can directly use the Nodejs core module in your application without any installation. Let’s see an example of the file system (fs) module to handle the files in the application.
Initially, the Project Directory will look like this:

Initially, the Project directory
Example: Write the below code in the index.js file
Javascript
const fs = require( 'fs' );
fs.writeFileSync( 'notes.txt' , 'I love to code' );
|
The script above uses require to load in the fs module. This is a built-in Node.js module that provides functions you can use to manipulate the file system. The script uses writeFileSync to write a message to notes.txt.
To run the script use the following command on your terminal
$ node index.js
After you run the script, you’ll notice a new notes.txt file in your directory. Open it up and you’ll see, “I love to code”.
Output: Now we have a new file notes.txt in our directory. And “I love to code” written has written on it.

Output
Here is the list of some Nodejs Core Modules:
Core Modules Name |
Description |
fs |
To handle the file system. |
http |
To make Node.js act as an HTTP server |
https |
To make Node.js act as an HTTPS server. |
os |
It provides information about the operation system. |
path |
To handle file paths. |
cluster |
To split a single Node process into multiple processes. |
dns |
To do DNS lookups and name resolution functions. |
tls |
To implement TLS and SSL protocols. |
querystring |
To handle URL query strings. |
url |
To parse URL strings. |
events |
To handle events |
timers |
To execute a function after a given number of milliseconds. |
Local Modules: Putting all your code in a single file is not a good idea. As you add more code, you’ll want to stay organized and break your Node.js app into multiple scripts that all work together. For that purpose, we can create local modules in our application.
Exporting from files: Firstly we need to create a file called utils.js. Now We can export javascript code written on this file using module.exports. In below a function is defined and assigned to the module.exports.
Project setup:
- Utils.js: Write the below code in the utils.js file
Javascript
const tests = function () {
console.log( "Yehh! Local file is running successfully..." );
}
module.exports = tests;
|
Importing your own files: Now we need to import the local file into index.js. require function is also used for importing the files in the directory. All you need to do is provide require with a relative path to the script you want to load. This path should start with ./ and then link to the file that needs to be loaded.
Filename:- In the index.js file
Javascript
const utility = require( './utils.js' );
utility();
|
The code above uses require to load in a file called utils.js in the src directory. It stores the module contents in a variable, and then use the contents in the script.
If you run the above index.js script. you will see the message that is logged from the tests function defined in the utils.js file.
Output:
Yehh! Local file is running successfully...
Third-party Modules: Third-party modules can be installed from the NPM (Node Package Manager) available online.
Firstly we need to initialize the npm using the npm init command before npm can be used. It creates a package.json file in the root directory and it stores all the information about the third-party module that we have installed as a dependency.
Installing an NPM module:
npm install "module_name"
The above command will do the following:
Firstly, It creates the node_modules directory which npm uses to store all the codes of the npm module you have installed. Secondly, npm add a module as a dependency by listing it in the dependencies property in the package.json file, and lastly, npm creates a package-lock.json file. This is used to store information about the modules you’ve installed which helps keep things fast and secure.
Examples of Third-Party Module:
- npm install express
- npm install lodash
- npm install mocha
Let’s see with an example how to use third party modules:
- validator is a popular module used for string validations and sanitizations.
To install run the following command:
$npm install validator
npm modules can be imported into your script using require. To load in an npm module, pass the npm module name to require:
index.js: Write the below code in the index.js file
Javascript
const validator = require( "validator" );
console.log( "Email is " + validator.isEmail( "johndoeeg.com" ));
|
Output:

Similar Reads
What are modules in Node JS ?
In NodeJS, modules are encapsulated units of code that can be reused across different parts of an application. Modules help organize code into smaller, manageable pieces, promote code reusability, and facilitate better maintainability and scalability of NodeJS applications. Types of Modules:Core Mod
2 min read
How to Create Modules in Node.js ?
Modules are the building blocks of NodeJS code. They help you break down your project into smaller, manageable pieces, each with its own job. To create a module, just make a JavaScript file and export what you want to share. Other files can then import and use those exports, adding that functionalit
3 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 Modules
In NodeJS, modules play an important role in organizing, structuring, and reusing code efficiently. A module is a self-contained block of code that can be exported and imported into different parts of an application. This modular approach helps developers manage large projects, making them more scal
6 min read
Node.js Assert Module
Assert module in Node.js provides a bunch of facilities that are useful for the assertion of the function. The assert module provides a set of assertion functions for verifying invariants. If the condition is true it will output nothing else an assertion error is given by the console. Assert Module
3 min read
What are the Modules in Typescript ?
Modules in TypeScript allow you to organize code into reusable, manageable, and logical units by encapsulating functionalities into separate files. They help avoid global namespace pollution by providing scoped declarations.Modules can be imported and exported, enabling code reuse and better maintai
4 min read
How to use EcmaScript Modules in Node.js ?
Using ECMAScript Modules (ES Modules or ESM) in Node.js allows you to take advantage of modern JavaScript syntax for organizing and managing your code. ECMAScript Modules provide a more structured and standardized way to work with modules compared to CommonJS, which has been traditionally used in No
2 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 21 is here: Whatâs new
Node.js continues to evolve at a rapid pace, and Node.js 21 is a testament to this commitment. This release offers a variety of improvements catering to developers of all levels. From the long-awaited stabilisation of the Fetch API to the introduction of a built-in WebSocket client, Node.js 21 empow
7 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