Node.js Modules
A module is a JavaScript file whose code you can use in other files. Modules keep big programs organised.
Three Kinds of Modules
| Kind | Example | How you get it |
|---|---|---|
| Built-in modules | fs, http, path, os | Come with Node.js |
| Your own modules | ./utils.js | Files you write |
| Third-party modules | express, dayjs | Installed with npm |
Node.js supports two module systems: CommonJS (the original) and ES modules (the modern JavaScript standard).
CommonJS: require and module.exports
Create math.js:
Example
// math.js
function addTax(price) {
return price + price * 0.18;
}
function discount(price, percent) {
return price - (price * percent) / 100;
}
module.exports = { addTax, discount };Use it in app.js with require:
Example
// app.js
const { addTax, discount } = require("./math");
console.log("With tax:", addTax(1000));
console.log("After 10% off:", discount(2500, 10));With tax: 1180 After 10% off: 2250
The ./ means "in this folder". Without it, Node.js looks for a built-in or installed module instead.
ES Modules: import and export
ES modules use the same import and export you know from front-end code. To use them, either name your files .mjs, or add "type": "module" to package.json:
Example
{
"name": "shop",
"type": "module"
}Then:
Example
// math.js
export function addTax(price) {
return price + price * 0.18;
}
export default function discount(price, percent) {
return price - (price * percent) / 100;
}Example
// app.js
import discount, { addTax } from "./math.js";
console.log(addTax(500));
console.log(discount(800, 25));590 600
With ES modules you must include the file extension (./math.js).
CommonJS vs ES Modules
| CommonJS | ES modules | |
|---|---|---|
| Import | const x = require("x") | import x from "x" |
| Export | module.exports = ... | export / export default |
| Turned on by | Default for .js files | "type": "module" or .mjs |
Top-level await | No | Yes |
__dirname | Yes | Use import.meta.dirname |
For new projects, ES modules are the modern choice. You will still see CommonJS in many older projects and tutorials, so it is good to know both.
Built-in Modules
Built-in modules can be imported with a node: prefix, which makes it clear they come from Node.js.
The os module
Example
import os from "node:os";
console.log("System:", os.platform());
console.log("CPU cores:", os.cpus().length);
console.log("Free memory (GB):", (os.freemem() / 1024 ** 3).toFixed(1));System: win32 CPU cores: 8 Free memory (GB): 5.3
The path module
Folder paths use \ on Windows and / on Linux. The path module handles this for you:
Example
import path from "node:path";
const file = path.join("reports", "2026", "october.csv");
console.log(file);
console.log(path.extname(file));
console.log(path.basename(file));reports/2026/october.csv .csv october.csv
(On Windows the first line shows reports\2026\october.csv.)
Module Caching
A module's code runs only once, the first time it is imported. Later imports get the same exported object. This is why a module can safely hold shared things, such as a database connection.
Exercise
Exercise
What must you add to package.json to use import and export in .js files?
