ES Modules and Live Bindings
ES Module imports do not copy values; they link read-only pointers straight into the exporting module's Execution Context slot. Because linking completes before execution, mutations in the exporter surface immediately to every importer, and static graphs enable tree-shaking.
CommonJS Value Copy vs ESM Live Binding
// counter.cjs
let count = 0;
module.exports = { count, inc: () => ++count };
// main.cjs
const { count, inc } = require('./counter.cjs');
inc(); // count is still 0 (copied primitive)Destructured primitives go stale immediately after mutation.
// counter.mjs
export let count = 0;
export const inc = () => ++count;
// main.mjs
import { count, inc } from './counter.mjs';
inc(); // count reads 1 via live slotImporter always dereferences the exporter's live LexicalEnvironment slot.
Under the Hood
During instantiation, V8 allocates a ModuleEnvironmentRecord per module and wires read-only indirect bindings straight to the exporter's slots before execution. Whereas CommonJS require() copies a value snapshot at call time, an ESM import exposes live updates without allowing reassignment.
- Construction: The loader fetches and parses all static
importspecifiers before running any code. - Instantiation: The engine allocates a
ModuleEnvironmentRecordfor each module and wires indirect bindings from imports to exports. - Evaluation: Modules execute post-order (leaves first); top-level
awaitpauses parent evaluation until settled. - Network Discovery: Unlike
<script>tags spotted by Preload Scanner Hints, deep ESM imports trigger waterfalls unless declared withmodulepreload.
// counter.mjs
export let count = 0;
export const increment = () => { count += 1; };
// consumer.mjs
import { count, increment } from './counter.mjs';
increment();
console.log(count); // 1 (live read-only view)Problem Genealogy
- 2009 (CommonJS & AMD): Synchronous
require()copiedmodule.exportsby value, breaking circular references and static dead-code elimination. - 2015 (ES2015 Modules): Standardized static
import/exportsyntax with live read-only bindings. - 2022 (
<link rel="modulepreload">& Import Maps): Eliminated bare-specifier tooling lock-in and sequential fetch waterfalls.
Production Pitfalls
- Circular TDZ Crashes: Reading an imported
constbefore the exporting module finishes evaluating throwsReferenceError. - Unbundled Waterfall Latency: Serving 200 raw ESM files over HTTP/2 without
modulepreloadstalls First Interactive behind serial round-trips.
- When not to use raw unbundled ESM: Never ship hundreds of unbundled
node_modulesESM files to production browsers without bundling or<link rel="modulepreload">hints.
Connections & Logical Next Steps
Import Maps map bare specifiers onto ES Module graph URLs while preserving live read-only bindings.
Import maps and Module Federation share singleton framework runtimes across micro-frontends via ES module bindings.
ModuleEnvironmentRecords extend LexicalEnvironments with cross-module indirect binding slots.
Nested ES module imports cannot be discovered by the HTML preload scanner without modulepreload hints.
Function lexical environments allocate per-call bindings, whereas ES Modules expose read-only live bindings across module records.
Seen in Architecture Autopsies
Mechanical Comparisons
Function lexical environments allocate per-call bindings, whereas ES Modules expose read-only live bindings across module records.
Nested ES module imports cannot be discovered by the HTML preload scanner without modulepreload hints.