Live bindings and module evaluation
How it works
An imported name reads the current value of the corresponding exported binding, rather than a snapshot of the value when the import occurred. A module may update its own mutable export. A consumer cannot reassign the imported binding. For a normal module graph, linking establishes import/export relationships before evaluation; a dependency is not reexecuted on every use of an imported function.
Example 1
Save each file separately. Run with Node.js: main.mjs. This is a multi-file example, not a single editor snippet.
counter.mjs
main.mjs
import { count, inc } from "./counter.mjs";const snapshot = count;inc(); inc();console.log(count, snapshot);Expected output in the tested environment:
2 0The import observes two updates, but the local snapshot remains zero.
Common mistake
Do not treat a live binding as permission to write through the import. Export an operation that performs the update in its owning module.
Practice
Add reset() to the exporter. Read count before and after reset from the importing module.
Run this practice in the documented local environment. ByteMind module and asynchronous grader support is not yet verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Live bindings and module evaluation — Knowledge Check
Reason about the concept and predict its behavior in the indicated environment.
Who may directly assign to the exported let count binding?
What does const snapshot = count preserve when count is a number?
When are static import/export links established relative to ordinary evaluation?
If two modules import the same resolved module in one normal graph, what is shared?
For Live bindings and module evaluation, what is the console output of example 1 in Node.js 22.16.0?
// File: counter.mjsexport let count = 0;export function inc() { count++; }
// File: main.mjsimport { count, inc } from "./counter.mjs";const snapshot = count;inc(); inc();console.log(count, snapshot);