JavaScript modules: import, export and ES modules in Node.js
Named versus default exports, how ES modules differ from CommonJS require, setting up Node.js for import, and how to organise a project into small modules.
A program that lives in one file gets hard to read fast. Modules let each file own one job and share only what others need.
Exporting and importing
// pricing.js
export const VAT = 0.21;
export function priceWithVat(net) {
return Math.round(net * (1 + VAT) * 100) / 100;
}
// main.js
import { priceWithVat, VAT } from "./pricing.js";
console.log(priceWithVat(10)); // 12.1Anything not exported stays private to its module. That's the main benefit: you can change a module's internals without breaking its users, as long as the exports stay the same.
Named vs default exports
export default function charge() {} // default: one per module
import chargeCard from "./charge.js"; // the importer picks any name
export function refund() {} // named: as many as you like
import { refund } from "./refund.js"; // must use the exported name (or `as`)Named exports are easier to find and refactor, so most teams prefer them. Rename on import when
names clash: import { refund as refundCard } from "./refund.js";.
ES modules in Node.js
Node.js supports two module systems:
| ES modules (ESM) | CommonJS (CJS) |
|---|---|
import / export | require() / module.exports |
.mjs, or .js with "type": "module" | .cjs, or .js without it |
| imports resolved before the code runs | require runs where it's called |
top-level await allowed | no top-level await |
For new projects, use ESM: add "type": "module" to package.json. Two differences to remember:
local imports need the file extension ("./pricing.js"), and __dirname doesn't exist (use
import.meta.dirname in Node.js 20.11+).
Organising a project
A structure that scales:
src/
domain/ pure logic: no I/O, easy to test (pricing.js, booking.js)
services/ talks to the outside world (http.js, storage.js)
main.js wires everything together
test/
pricing.test.jsKeep the domain free of network and file access, and pass services in as parameters. Your core logic is then testable with plain function calls.
Avoid circular imports
If a.js imports b.js and b.js imports a.js, one of them sees the other half-initialised.
Symptoms are undefined imports or "cannot access before initialization" errors. Usually the fix
is to move the shared piece into a third module both can import.
Common mistakes
- Forgetting the
.jsextension in ESM imports. - Mixing
requireandimportin the same file. - One giant
utils.jsthat everything imports: split it by topic.
The JavaScript projects in the JavaScript Developer career path are ES modules from the first milestone.
Questions and answers
- Should I use named exports or default exports?
- Prefer named exports. They keep the same name everywhere they're imported, work better with editor auto-import and refactoring, and let a module export several things without special cases.
- How do I use import in Node.js?
- Either name the file .mjs, or add "type": "module" to package.json so .js files are treated as ES modules. Local imports then need the file extension, e.g. import { price } from './pricing.js'.
Practise it
Read next
- Testing JavaScript with node:test: no framework needed
Write and run JavaScript tests with Node.js's built-in test runner: test and describe, assert.strictEqual vs deepStrictEqual, async tests, mocks and what makes a good test.
- JavaScript promises and async/await: a practical guide
How promises work, what await really waits for, running tasks in parallel with Promise.all, handling errors properly, and the event loop in two minutes.