Node.js Tutorial menu

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

KindExampleHow you get it
Built-in modulesfs, http, path, osCome with Node.js
Your own modules./utils.jsFiles you write
Third-party modulesexpress, dayjsInstalled 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

js
// 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

js
// app.js
const { addTax, discount } = require("./math");

console.log("With tax:", addTax(1000));
console.log("After 10% off:", discount(2500, 10));
Output
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

json
{
  "name": "shop",
  "type": "module"
}

Then:

Example

js
// math.js
export function addTax(price) {
  return price + price * 0.18;
}

export default function discount(price, percent) {
  return price - (price * percent) / 100;
}

Example

js
// app.js
import discount, { addTax } from "./math.js";

console.log(addTax(500));
console.log(discount(800, 25));
Output
590
600

With ES modules you must include the file extension (./math.js).

CommonJS vs ES Modules

CommonJSES modules
Importconst x = require("x")import x from "x"
Exportmodule.exports = ...export / export default
Turned on byDefault for .js files"type": "module" or .mjs
Top-level awaitNoYes
__dirnameYesUse 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

js
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));
Output
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

js
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));
Output
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?

Learn Node.js with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp