Node.js Modules

Node js 8 min min read Updated: Mar 29, 2026 Beginner
Node.js Modules
Beginner Topic 4 of 12

Node.js Modules (CommonJS System)

Modules are one of the most important concepts in Node.js. They help developers organize code into smaller, reusable, and maintainable pieces. Instead of writing everything in a single file, you can divide your application into multiple files (modules), each handling a specific functionality.

Node.js uses the CommonJS module system by default. This system allows you to export functionality from one file and import it into another file using simple syntax.

Key Concept: Modules allow you to split your code into smaller parts and reuse them across your application.

What are Modules in Node.js?

A module is simply a file that contains code. In Node.js, every file is treated as a separate module. This means variables, functions, and objects defined inside a file are not accessible outside unless explicitly shared.

This behavior helps prevent conflicts and keeps your code clean and structured.

Types of Modules in Node.js

1. Core Modules

Core modules are built into Node.js. You do not need to install them separately. They provide essential functionalities like file handling, HTTP servers, path utilities, and more.

Example of a core module:

javascript const fs = require("fs");

In this example, fs (File System) is a core module used for working with files such as reading, writing, updating, and deleting files.

2. Local Modules

Local modules are modules that you create in your own project. You can export functions, objects, or variables from one file and import them into another file.

Example:

math.js

javascript function add(a, b) { return a + b; } module.exports = add;

app.js

javascript const add = require("./math"); console.log(add(2, 3));

This example shows how a function defined in one file can be reused in another file.

3. Third-Party Modules

Third-party modules are installed using npm (Node Package Manager). These modules are created by other developers and can be used to speed up development.

Example:

bash npm install express
javascript const express = require("express");

Here, express is a popular third-party module used to build web servers and APIs.

Understanding require()

The require() function is used to import modules in Node.js. When you call require(), Node.js loads the module and makes its exported content available to your file.

javascript const fs = require("fs");

This line imports the built-in fs module so you can use its methods in your code.

Understanding module.exports

In Node.js, module.exports is used to export functions, objects, or variables from a module. Whatever you assign to module.exports becomes available when the module is imported using require().

javascript function greet(name) { return "Hello " + name; } module.exports = greet;

Now this function can be reused in any other file by importing it.

Exporting Multiple Values

You can also export multiple functions or variables using an object.

javascript function add(a, b) { return a + b; } function subtract(a, b) { return a - b; } module.exports = { add, subtract };

And import them like this:

javascript const math = require("./math"); console.log(math.add(5, 2)); console.log(math.subtract(5, 2));

Why Modules are Important

  • Code organization: Break large applications into smaller files
  • Reusability: Use the same code in multiple places
  • Maintainability: Easier to update and manage code
  • Scalability: Helps in building large applications efficiently

Module Scope in Node.js

Each module in Node.js has its own scope. This means variables and functions defined inside a module are private by default. They are not accessible outside unless you export them.

This feature helps avoid naming conflicts and keeps your application safe and modular.

Common Mistakes

  • Forgetting to export functions using module.exports
  • Using incorrect file paths in require()
  • Mixing ES Modules (import/export) with CommonJS incorrectly

Modules vs ES Modules (ESM)

Node.js also supports ES Modules (ESM) using import and export. However, the default system is still CommonJS in many projects.

Example of ES Module:

javascript import fs from "fs";

While ES Modules are modern and widely used, many existing Node.js applications still rely on CommonJS.

Conclusion

Modules are a fundamental part of Node.js that allow developers to write clean, reusable, and maintainable code. By using the CommonJS module system with require() and module.exports, you can easily organize your application into smaller components.

Understanding modules is essential for building scalable Node.js applications, and it forms the foundation for working with frameworks like Express.js.

Quick Summary: Node.js modules let you split your code into reusable files using the CommonJS system with require() and module.exports.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators