How To Create Modules in NodeJS? Last Updated : 19 Feb, 2025 Comments Improve Suggest changes Like Article Like Report 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 ModulesNodeJS 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 NodeJSTo 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 ModuleTo 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 ModuleNow 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 NodeJSUse meaningful and descriptive names for your module files.Keep your modules focused on a single responsibility (SRP).Avoid global variables in your modules. Comment More infoAdvertise with us Next Article How To Create Modules in NodeJS? A ashitace696 Follow Improve Article Tags : Web Technologies Node.js Node.js-Misc 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 How to add new functionalities to a module in Node.js ? Node.js is an open-source and cross-platform runtime environment built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. You need to recollect that NodeJS isnât a framework, and itâs not a programming language. In this article, we will discuss how to add new functi 3 min read NodeJS 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 How to write code using module.exports in Node.js ? module is a discrete program, contained in a single file in Node.js. They are tied to files with one module per file. module.exports is an object that the current module returns when it is "required" in another program or module. We are going to see a simple code like the calculator to learn how to 3 min read Modules in NestJS NestJS is a progressive Node.js framework that has gained significant popularity for building efficient, reliable, and scalable server-side applications. One of its core concepts is the use of modules, which help in organizing the application. In this article, weâll learn what modules are, why they 3 min read Like