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

Module Boundaries: ESM and CommonJS

Modules in JavaScript 25 min

javascript / Module Boundaries: ESM and CommonJS

Explore each scene at your own pace.

01 · Module Boundaries: ESM and CommonJS

Module Boundaries: ESM and CommonJS

Module Boundaries: ESM and CommonJS

A module defines a scope and an explicit public interface. ESM connects imports to exported bindings; CommonJS exposes the value of module.exports through require. File type and host configuration determine which loader interprets a file.

Put ownership in one module and export a small interface instead of sharing accidental globals across files.

Give state an owner

A reading list needs a total that several views can inspect. A module can own that state and export operations that change it. Separating ownership makes it possible to explain what another file may read, what it may call, and what it must not mutate directly.

Walk through the modelModule Boundaries: ESM and CommonJSAuthored snapshots of the example; use Run below to test the code.
private scopecount
public interfacecount binding + increment()
One module owns mutable state and exports operations.
1 of 3

Run an actual ESM connection in the browser

html

The demo creates two temporary JavaScript module resources with blob URLs. They are separate modules loaded by the browser's module loader, not two concatenated scripts pretending to be modules. The consumer imports the provider's exported binding and function, reads the count, invokes increment, and reads again. Revoking the URLs after the module has loaded releases the temporary URL registrations. A real project would normally use separate .js or .mjs files served by an HTTP server.

An imported binding is not a writable local variable owned by the consumer. The provider may update its exported let binding and the consumer observes the update. Importing it does not give the consumer permission to assign count = 10. A const-exported object is different: its referenced object may still be mutable unless the interface prevents that.

Compare with two real CommonJS files

Save the following as counter.cjs:

let count = 0;
module.exports = { increment() { count += 1; }, read() { return count; } };

Save the following as main.cjs in the same directory:

const counter = require('./counter.cjs');
counter.increment();
console.log(counter.read());

Run node main.cjs in that directory. It prints 1. Requiring the same resolved module normally returns its cached exports rather than executing a fresh module for every call. There are loader and resolution details around that statement; do not generalize it to every different path or every process. Node can also load ESM. Use .cjs for an explicit CommonJS example and .mjs for an explicit ESM example instead of relying on unspecified project defaults.

In an ESM file, relative imports generally need explicit file extensions in the browser and Node's standard ESM resolution. Browsers do not natively resolve a package name such as 'some-library' without an import map or other tooling. A module script has its own scope and strict semantics. Serving files over HTTP avoids restrictions associated with opening module examples directly from file URLs.

Predict and repair

Before importing, predict the three printed counts. Then change the provider to export a snapshot constant instead of the live count binding and explain the changed contract. In the CommonJS version, replace module.exports with an assignment to exports and inspect why require does not receive that reassigned local alias.

The graded task tests the public state-owner contract through closures. It does not claim to test a filesystem loader. The runnable browser example and the two-file Node exercise test the actual loaders separately.

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.