Skip to the workspace
Pebellslearn by building
Modules in JavaScript  ›  Lesson

ESM Modules

ESM 15 min

javascript / ESM Modules

Explore each scene at your own pace.

01 · ESM Modules

ESM Modules

ESM Modules

ES modules declare exports and import bindings from other modules. Each module has its own scope, runs in strict mode, and is loaded by its host.

State a dependency instead of relying on another file to create an accidental global name.

See the mechanism

Walk through the modelESM ModulesAuthored snapshots of the example; use Run below to test the code.
export title"Morning"
The module explicitly exposes one binding.
1 of 3

Follow one example

html

The generated module exports title, and dynamic import returns a promise for its module namespace. This single-file preview uses a temporary Blob URL so it needs no server or second editor. In a normal project, place export const title = "Morning" in article.js and use import { title } from "./article.js" in a module entry. Named imports refer to exported bindings; a default export is imported with different syntax. Serve browser files through an HTTP development server rather than assuming file:// loading behaves the same.

Predict, change and check

Predict: If the export is renamed to headline but the consumer still reads article.title, what result should it see?

Test your explanation: Rename both producer and consumer deliberately. Then add a second named export. Compare a declared module dependency with writing window.title.

The tasks below ask you to explain that result and apply the idea to different inputs.

Apply and distinguish

Imports are module connections

ESM imports connect to exported bindings and do not simply paste another file's text into the importer. A provider can update an exported let binding while consumers observe it, but an importer cannot reassign that binding directly. Module scope and strict semantics differ from classic global scripts.

Relative specifiers need to resolve to actual served module resources, with explicit extensions in common browser and standard Node ESM cases. Bare package specifiers need an appropriate host resolver, import map or tooling. Test a real import/export boundary instead of assuming a plain single-file helper test verifies module loading.

Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.