Node Modules
Last Updated : Oct 2026
In Node.js, you don't write all your code in one file. You split it into multiple files called modules. Modules are three types:
- User-Defined Modules: Created by you.
- Built-in Modules: Provided by the language itself.
- Third-Party Modules: External libraries you install using npm or yarn.
User - Defined Modules
You can create your own modules by writing JavaScript code in separate .js files.
Node has two ways to import and export code between files:
- CommonJS (require / module.exports) — The traditional, default way Node worked for years.
- ES Modules (import / export) — The modern JavaScript standard (same syntax used in React/Vue/frontend).
1. CommonJS (The Node.js Classic)
By default, Node treats .js files as CommonJS modules.
- You export values by attaching them to module.exports or exports. before the value.
- You import values using the require() function.
- require() loads files synchronously (at runtime).
Example
Create a helper file named mathCJS.js:
// mathCJS.js
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
exports.subtract = (a, b) => a - b;
// Exporting an object containing our functions
module.exports = {
add,
multiply,
};
Now, create indexCJS.js in the same directory:
// indexCJS.js
// Import using relative path './'
const math = require("./mathCJS");
// Destructuring :
// const { add, multiply } = require("./mathCJS");
console.log("Add:", math.add(5, 3));
console.log("Multiply:", math.multiply(4, 2));
// if you use Destructuring :
// console.log("Add:", add(5, 3));
// console.log("Multiply:", multiply(4, 2));
Run it in your terminal:
node indexCJS.js
2. ES Modules (ESM - The Modern Standard)
ES Modules use import and export statements. They are processed statically (before the code runs), making them cleaner and enabling optimizations like tree-shaking (removing unused code).
To use ESM in Node.js, you have two choices:
- Option A: Name your files with .mjs extension (e.g., m.mjs).
- Option B (Recommended): Add "type": "module" inside your package.json file. This tells Node to treat all .js files in the folder as ES Modules.
Example
Option A
Create a helper file named m.mjs:
// math.mjs
export function add(a, b) {
return a + b;
}
export function multiply(a, b) {
return a * b;
}
Now, create index.mjs in the same directory:
// index.mjs
import { add, multiply } from "./math.mjs";
// Note: In ESM Node.js, you MUST include the file extension!
console.log("Add:", add(5, 3));
console.log("Multiply:", multiply(4, 2));
Run it in your terminal:
node index.mjs
Option B
Step 1: Initialize package.json
Run this in your terminal:
npm init -y
Open package.json and add "type": "module" at the top level:
{
"name": "your-project-name/directory-name",
"version": "1.0.0",
"type": "module",
"main": "index.js"
}
Step 2: Write ESM Code
Create mathESM.js:
// mathESM.js
// Named Exports
export function add(a, b) {
return a + b;
}
export function multiply(a, b) {
return a * b;
}
// Default Export (optional main export)
export default function subtract(a, b) {
return a - b;
}
Create indexESM.js:
// indexESM.js
import subtract, { add, multiply } from "./mathESM.js";
console.log("Add:", add(10, 5)); // 15
console.log("Multiply:", multiply(3, 3)); // 9
console.log("Subtract:", subtract(10, 4)); // 6
Run it in your terminal:
node indexESM.js
