Skip to content

ES modules (ESM)

ES modules (ESM) are the module system built into JavaScript, in which each file declares the values it shares with export and the values it needs with import, so the engine can connect the files before any of their code runs.

ECMAScript modules arrived in ES2015. Files connect through import and export declarations, which can appear only at the top level of a module. Each module has its own scope, so a top-level declaration stays private unless the module exports it. Module code always runs in strict mode, and top-level this is undefined.

Import declarations are hoisted. The whole dependency graph is loaded and linked first, and then each module runs once. An imported name is a live binding. It reflects the current value of the exporting module’s variable, but the importing module can’t reassign it. Modules can also use top-level await. For the two files in the example below, those steps happen in this order:

The engine loads main.mjs and tracker.mjs and links imports, then runs tracker.mjs once and main.mjs once.
Load and Link Before Any Module Runs

The language defines the syntax, and each host decides which files are modules. Browsers load a file as a module through <script type="module">. Node.js treats .mjs files, and .js files under a package.json with "type": "module", as ES modules. Node.js also still runs CommonJS, its older module system.

Example

Say a page-view tracker keeps its running total in one module, and another module reads it:

Language: JavaScript Filename: tracker.mjs
const step = 1;
export let views = 0;

export function recordView() {
  views += step;
}

The entry module imports the counter and the function, then tries to change the counter itself:

Language: JavaScript Filename: main.mjs
import { views, recordView } from "./tracker.mjs";

console.log(`Before: ${views}`);
recordView();
recordView();
console.log(`After: ${views}`);
console.log(`step: ${typeof step}`);
console.log(`this: ${this}`);
views = 10;

Running the entry module shows the live binding and the read-only import:

Language: Shell
$ node main.mjs
Before: 0
After: 2
step: undefined
this: undefined
TypeError: Assignment to constant variable.

Both calls to recordView() changed views inside tracker.mjs, and main.mjs read the new value through the same import. The step constant stays inside tracker.mjs because the module never exports it. The final assignment throws a TypeError, because an imported binding is read-only for the importer.

A developer feeds a module into a connected machine labeled Import, ESM, Module, CJS, and Export on a blue background.

Course

From Python Packages to JavaScript Modules: ESM and CommonJS

Organize larger JavaScript programs with ES modules, shared state, dynamic imports, and CommonJS while porting a Python heating digest.

intermediate js py

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 18, 2026