Sub topic 1
Recap: Static Imports and the Module Graph
Dynamic import and top-level await both change how ES modules load. Before covering those changes, this chapter reviews the default behavior: static import declarations and the module graph they produce.
Prerequisites
This chapter assumes you already know:
- ES module syntax with
importandexport - Promises and
async/await - How to run a module in a browser or in Node.js
- How npm packages are installed and referenced
The next chapter covers how browsers and Node.js decide that a file is a module. In the examples here, every file is assumed to be an ES module.
What Makes an Import "Static"
A static import is an import declaration written at the top level of a module. It has three fixed properties:
- Top-level only. It cannot appear inside a function, an
ifblock, or a loop. - Literal specifier. The path must be a plain string such as
'./math.js'. A variable or template literal will not work. - Hoisted. The runtime processes every import before any code in the module runs. This holds even when the declaration sits below other statements.
Because of these rules, a tool can read the source text alone and know every dependency. It does not have to run anything. Bundlers, linters, and tree-shaking depend on this.
// math.js
export function add(a, b) {
return a + b;
}
export let callCount = 0;
export function addTracked(a, b) {
callCount += 1;
return add(a, b);
}// main.js
import { addTracked, callCount } from './math.js';
console.log(callCount); // 0
console.log(addTracked(2, 3)); // 5
console.log(callCount); // 1The second log of callCount prints 1. Imports are live bindings, which means read-only views of the exporting module's variables. They are not copies. When math.js updates callCount, main.js sees the new value. If main.js assigns to callCount, it throws a TypeError.
The Module Graph
The runtime follows every static import, starting from the entry module, and builds a module graph. Each node in the graph is one module, and each edge is one import. Loading the graph takes three phases:
- Construction. The runtime fetches and parses each module, finds its imports, and repeats for those imports until it reaches modules with no new dependencies.
- Linking. The runtime matches every import to the export it names. A missing export, such as importing
{ subtract }when no module exports it, fails at this step. No code has run yet. - Evaluation. The runtime runs each module's top-level code once, in depth-first post-order. Dependencies run before the modules that import them.
In this graph, utils.js is imported twice but evaluated only once. Every importer receives the same module instance.
The Costs of Static Imports
Static imports are predictable, but they come with trade-offs:
| Property | Consequence |
|---|---|
| Whole graph loads first | Startup waits for code the user may never touch |
| One failure fails all | A single missing file or bad export stops the whole application |
| Specifier is fixed | You cannot choose a module from runtime data, such as the user's language |
| Evaluation is synchronous | A module cannot wait for async work before its importers run |
The import() expression addresses the first three rows. Top-level await addresses the last one. Later chapters cover both.
Exercise
Module a.js contains console.log('a'); import './b.js'; and b.js contains console.log('b');. What does running a.js print, and why?
Show answer
It prints "b" and then "a". Import declarations are hoisted, so b.js is evaluated before any code in a.js runs, even though the import is written after the console.log call.
Exercise
Why can't you write import { t } from \./locales/${lang}.js\; as a static import?
Show answer
A static import needs a string-literal specifier, so the module graph can be built before any code runs. A template literal depends on a runtime value, and that value does not exist during the construction phase.