Modules in Node.js allow us to organize JavaScript code into separate files and reuse functions, variables, classes, and other functionality.
Instead of writing an entire application in one large file, we can divide it into smaller, manageable modules. This makes our code easier to understand, maintain, and reuse.
A module is a file or a collection of code that provides specific functionality. For example, one module can perform mathematical calculations while another can manage student information.
MyNodeProject/
├── app.js
├── math.js
└── student.js
In this project, math.js can contain
calculation functions, while student.js
can contain student-related functionality.
Write a function once and reuse it in different files.
Divide a large application into smaller files with clear responsibilities.
Update one module without unnecessarily changing unrelated code.
Built-in modules are provided by Node.js. They offer functionality without requiring you to install a separate package.
fs – File system operations.path – Working with file and directory paths.os – Operating-system information.http – Creating HTTP servers and working with HTTP.Local modules are files created by you inside your own project. You can export functionality from one file and import it into another.
Third-party packages are created and shared by other developers. They are commonly installed using NPM, for example, Express.js and Lodash.
Let's use the built-in os module to display information
about the operating system.
const os = require('node:os');
console.log("Platform:", os.platform());
console.log("Architecture:", os.arch());
Platform: win32
Architecture: x64
The output above is an example from a Windows computer. Your results may differ depending on your operating system and hardware.
require('node:os') loads the built-in OS module using CommonJS syntax.os.platform() returns the operating-system platform identifier.os.arch() returns the architecture of the Node.js process.
Now let's create a local module named math.js.
It will contain a function for adding two numbers.
function add(a, b) {
return a + b;
}
module.exports = add;
The module.exports statement makes the
add function available to other CommonJS modules.
const add = require('./math');
console.log(add(10, 20));
console.log(add(5, 15));
30
20
require('./math') loads the local module from math.js.module.exports = add exports the function.add(10, 20) returns 30.add(5, 15) returns 20../ indicates a local
file relative to the current module. It is not the same as importing
an NPM package by its package name.
A module can export multiple functions using an object.
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
module.exports = {
add,
subtract
};
const math = require('./math');
console.log("Addition:", math.add(20, 10));
console.log("Subtraction:", math.subtract(20, 10));
Addition: 30
Subtraction: 10
Node.js also provides a built-in file system module. Here is a simple example that creates a text file.
const fs = require('node:fs');
fs.writeFileSync('message.txt', 'Hello from Node.js!');
console.log("File created successfully.");
File created successfully.
When you run this program, it creates or replaces
message.txt in the current working directory with the
specified text. Be careful: writeFileSync() overwrites
an existing file with the same name.
Node.js supports two major JavaScript module systems: CommonJS and ECMAScript Modules (ESM).
CommonJS commonly uses require() and module.exports.
const add = require('./math');
console.log(add(2, 3));
ES Modules use import and export.
import { add } from './math.js';
console.log(add(2, 3));
This example assumes math.js exports a named
function and the project is configured to use ES modules,
for example with "type": "module" in
package.json.
If your module is in the same directory as
app.js, use:
require('./math');
A function declared in one CommonJS file is not automatically
available in another file. Export it using
module.exports or exports.
CommonJS and ES Modules use different syntax and configuration. Follow the module system configured for your project.
ModulePractice.math.js.app.js.app.js.os module to display your operating-system platform.1. What is a module in Node.js?
2. Which statement exports a function in CommonJS?
3. Which prefix indicates a relative local module path?