How To Create Modules in NodeJS?
Last Updated :
19 Feb, 2025
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 functionality to their code.
Types of Modules
NodeJS has three types of modules:
- Built-in Modules: Provided by NodeJS (e.g., fs, http, path).
- User-defined Modules: Custom modules created by developers.
- Third-party Modules: Modules installed via npm (e.g., express, lodash).
Steps to Create Modules in NodeJS
To create modules in NodeJS, write functions, objects, or classes in a separate file and use module.exports to export them. Import these modules in other files using the require() function for reuse.
Step 1: Creating a Module
To create a module, you simply need to write code in a separate file. You can then export specific variables, functions, or objects from that file to be used in other parts of your application.
Let’s start by creating a simple module that performs some basic mathematical operations.
File name: calc.js
javascript
exports.add = function (x, y) {
return x + y;
};
exports.sub = function (x, y) {
return x - y;
};
exports.mult = function (x, y) {
return x * y;
};
exports.div = function (x, y) {
return x / y;
};
- The calc.js file defines four functions: add, sub, mult, and div.
- These functions are exported as properties of the module.exports object, making them accessible to other files.
Step 2: Using a Module
Now that we’ve created a module, we can import it into another file and use the exported functions.
File name: App.js
javascript
const calculator = require('./calc');
let x = 50, y = 20;
console.log("Addition of 50 and 20 is "
+ calculator.add(x, y));
console.log("Subtraction of 50 and 20 is "
+ calculator.sub(x, y));
console.log("Multiplication of 50 and 20 is "
+ calculator.mult(x, y));
console.log("Division of 50 and 20 is "
+ calculator.div(x, y));
- The app.js file imports the calc.js module using require('./calc').
- It then uses the imported calculator object to perform arithmetic operations and logs the results.
Output
Create Modules in NodeJSBest Practices of Creating Modules in NodeJS
- Use meaningful and descriptive names for your module files.
- Keep your modules focused on a single responsibility (SRP).
- Avoid global variables in your modules.
Similar Reads
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
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
What are Modules in Node.js ? 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 t
5 min read
How to Change the Node.js Module Wrapper ? Changing the Node.js module wrapper involves customizing the way modules are wrapped by modifying the Module.wrap method. This allows for altering the function wrapper used in module loading. Module Wrapper FunctionUnder the hood, NodeJS does not run our code directly, it wraps the entire code insid
2 min read
How to use External Modules and NPM in a project ? Need for External Modules: For a large JavaScript application, it becomes difficult and messy to write the whole code in just one JavaScript file. This is where CommonJS comes into the picture and this CommonJS format defines a module format that can be used up for breaking your JS application into
3 min read
How To Create a Simple HTTP Server in Node? NodeJS is a powerful runtime environment that allows developers to build scalable and high-performance applications, especially for I/O-bound operations. One of the most common uses of NodeJS is to create HTTP servers. What is HTTP?HTTP (Hypertext Transfer Protocol) is a protocol used for transferri
3 min read