CommonJS is a module system used in Node.js. It allows us to divide a large program into smaller files and share functions, variables, objects, and other code between them.
CommonJS is a traditional module system supported by Node.js. It commonly uses two important features:
require() – loads a module.module.exports – exports values from a file.const os = require('node:os');
console.log(os.platform());
console.log(os.arch());
The require() function loads Node.js's built-in
OS module. The module provides information about the
operating system.
The require() function loads a module so that its
exported values can be used in the current file.
const path = require('node:path');
const filePath = path.join('students', 'data.txt');
console.log(filePath);
Possible output on Windows:
students\data.txt
The path module helps us work with file and
directory paths. The exact path separator depends on the
operating system.
We can create our own JavaScript file and export a function from it.
Another file can then import that function using
require().
function add(a, b) {
return a + b;
}
module.exports = add;
Here, module.exports = add; exports the
add() function from this file.
const add = require('./math');
console.log(add(10, 20));
Output:
30
The ./math path refers to the local
math.js file. Node.js can resolve the
.js extension automatically in this case.
We can export multiple functions by placing them inside an object.
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
function multiply(a, b) {
return a * b;
}
module.exports = {
add,
subtract,
multiply
};
const calculator = require('./calculator');
console.log(calculator.add(10, 5));
console.log(calculator.subtract(10, 5));
console.log(calculator.multiply(10, 5));
Output:
15
5
50
Node.js also provides an exports shortcut that initially
refers to the same export object as module.exports.
We can use it to add properties to that object.
exports.name = 'Rahul';
exports.age = 20;
In another file:
const student = require('./student');
console.log(student.name);
console.log(student.age);
Output:
Rahul
20
module.exports = value when exporting a
single function or replacing the entire exported value.
Do not assign a new value directly to
exports, such as exports = add.
That does not replace module.exports.
CommonJS can load different kinds of modules:
| Module Type | Example | Purpose |
|---|---|---|
| Built-in | require('node:fs') |
Work with files. |
| Built-in | require('node:path') |
Work with paths. |
| Local | require('./math') |
Load your own file. |
| Installed package | require('express') |
Load an installed package that supports CommonJS. |
math.js and app.js.math.js.require() in app.js.node app.js
require() to load modules and
module.exports to export values. A file using the
.cjs extension is explicitly treated as CommonJS.
A .js file's module system also depends on the
project's configuration.
student.js.getStudentName() that returns "Amit".module.exports.app.js.require('./student').console.log().Expected output:
Amit
Select the correct answer for each question.
1. Which function is commonly used to load a CommonJS module?
2. Which property is used to export a function from a CommonJS file?
3. Which path loads a local file named math.js?