Open In App

How To Create Modules in NodeJS?

Last Updated : 19 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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

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

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 Module - output

Create Modules in NodeJS

Best 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.

How To Create Modules in NodeJS -FAQs

Why should I create modules in NodeJS?

Creating modules helps to organize your code, making it reusable and easier to maintain.

How can I share my functions between files in NodeJS?

You can use module.exports to export functions from a module and require() to import them into another file.

Can I export objects and variables along with functions in NodeJS?

Yes, you can export objects, variables, and functions using module.exports in NodeJS.

What’s the difference between local and core modules?

Local modules are custom modules you create, while core modules are pre-installed with NodeJS, like fs and http.

How do I prevent namespace pollution in my NodeJS modules?

By exporting only the necessary functions or objects and avoiding global variables, you can prevent namespace pollution.



Next Article

Similar Reads

three90RightbarBannerImg