Sub topic 1
How ES Modules Load: A Quick Recap
JSON modules are ES modules, so they go through the same loading pipeline as any JavaScript module. This chapter reviews that pipeline so the later chapters about JSON-specific rules have a model to build on.
Prerequisites
This chapter assumes you already know:
- JavaScript ES modules, meaning
importandexportdeclarations - JSON syntax and
JSON.parse - Async/await and Promises
- How to run JavaScript in a browser (with
<script type="module">) or in Node.js (with.mjsfiles or"type": "module"inpackage.json)
The Three Loading Phases
When a host such as a browser or Node.js loads an entry module, it does not run the file line by line from the top. It works through three phases across the whole module graph. The module graph is the set of modules connected by import statements.
- Construction. The host resolves each import specifier (such as
"./math.js") to a URL or file path. It fetches the file and parses it into a module record. Parsing reveals moreimportdeclarations, so the host repeats the process for each one until it has found every module in the graph. - Linking. The host creates the exports of every module and connects each import to its matching export. Imports become live bindings that point at the exporting module's variables. They are not copies. If an import names an export that does not exist, linking fails before any code runs.
- Evaluation. The host runs each module's top-level code once. Dependencies run before the modules that import them, in depth-first order.
Seeing the Phases in Action
Save these two files in the same folder and run node main.mjs.
// counter.mjs
console.log("counter.mjs evaluated");
export let count = 0;
export function increment() {
count += 1;
}// main.mjs
import { count, increment } from "./counter.mjs";
console.log("main.mjs evaluated");
console.log(count); // 0
increment();
console.log(count); // 1: a live binding, not a copyThe output starts with counter.mjs evaluated, even though main.mjs is the entry point. The dependency is evaluated first. The second count log prints 1 because the import is bound to the exporting variable.
The Module Map and Caching
Each module graph has a module map. The module map is a cache that maps each resolved URL or path to its module record. If ten files import ./counter.mjs, the host fetches, links, and evaluates it once, and all ten importers share the same instance. Later chapters rely on this rule when explaining why JSON module data is shared.
Static and Dynamic Loading
| Form | When the module is found | Result |
|---|---|---|
| `import x from "./a.js"` | During construction, before any code runs | Bindings available at top level |
| `import("./a.js")` | At runtime, when the call executes | A Promise for the module namespace object |
Static imports let the host plan the whole graph ahead of time. Dynamic import() starts a new load at runtime, but it goes through the same three phases and uses the same module map.
Where Import Attributes Fit
During construction, the host only has a specifier to work with when it decides how to fetch and parse a file. Import attributes add extra information to the import, and the host reads that information during this phase. The following chapters show how with { type: "json" } uses this.
Exercise
Module a.mjs contains import { b } from "./b.mjs"; console.log("a");, and b.mjs contains console.log("b"); export const b = 1;. Both c.mjs and a.mjs import b.mjs, and the entry file runs import "./a.mjs"; import "./c.mjs";. How many times is "b" printed, and in what order do "a" and "b" appear?
Show answer
"b" is printed once, before "a". The module map caches b.mjs, so it is evaluated a single time, and because it is a dependency of a.mjs it runs before a.mjs.
Exercise
A module imports { total } from a file that only exports sum. In which phase does the failure occur, and does any top-level code in the graph run?
Show answer
The failure occurs during linking, when the host tries to connect the import to an export. No code runs, because evaluation never starts.