Sub topic 12
How Modules Are Linked and Evaluated
This chapter assumes you know the import and export forms from earlier chapters and can run a module in Node.js or a browser. It explains the order in which a module graph loads. That order explains why imports behave as they do.
The Three Phases
An engine does not run a module as soon as it reads it. It handles the whole module graph in three phases:
- Construction (parsing). The engine fetches the entry module and parses it. It reads the
importdeclarations and fetches and parses each dependency in turn. No module code runs during this phase. - Linking. The engine creates an environment for each module and connects every import to the matching export binding. A missing export causes a
SyntaxErrorhere, before any code runs. - Evaluation. The engine runs each module's top-level code once, dependencies first.
The engine can only find every dependency before running anything because import declarations are static. They must appear at the top level and use string literals.
Evaluation Order
The engine evaluates modules depth-first in post-order. Each module runs after all of its dependencies have run. Imports are also hoisted: the module's code runs only after its imports are ready, wherever the import lines appear in the file.
// a.js
console.log('a evaluated');
export const name = 'a';
// b.js
import './a.js';
console.log('b evaluated');
// main.js
console.log('main starts');
import './b.js';
import { name } from './a.js';
console.log('main ends', name);Running node main.js (with "type": "module" set) prints:
a evaluated
b evaluated
main starts
main ends aThe line main starts appears third, even though it comes before the imports in the source. Also, a.js runs once, although two modules import it.
Modules Are Singletons
Each module specifier resolves to one URL, and each URL gets one module instance per realm (a page, a worker, or a Node.js process). Every importer shares that instance, so shared state stays consistent:
// cache.js
export const cache = new Map();
// writer.js
import { cache } from './cache.js';
cache.set('user', 'Ada');
// main.js
import './writer.js';
import { cache } from './cache.js';
console.log(cache.get('user')); // AdaCommon Errors
| Symptom | Phase | Cause |
|---|---|---|
| `SyntaxError: ... does not provide an export named` | Linking | The imported name is not exported |
| Code before an `import` runs late | Evaluation | Imports are hoisted |
| State is not shared between files | Resolution | Two specifiers resolve to different URLs |
| A module's setup code never re-runs | Evaluation | The module instance is cached after its first evaluation |
Exercise
main.js imports './x.js' and then './y.js'. y.js also imports './x.js'. Each file logs its own name at the top level. What is printed when main.js runs?
Show answer
"x", then "y", then "main". x.js runs first because it is main's first dependency. y.js runs next, and its import of x.js reuses the cached instance, so x is not printed again. main.js runs last.
Exercise
You import { totl } from './math.js', but math.js only exports total. Does any code in math.js run before the error?
Show answer
No. The engine finds the missing export during linking, which happens before evaluation. It throws a SyntaxError and no module in the graph runs.