JavaScript · Question 83
What are live bindings in ES modules, and how do they affect circular dependencies?
Direct answer
ES module imports are live read-only views of exported bindings rather than one-time copied values; this allows updates to be observed across modules, but cycles can expose bindings before initialization and therefore require careful dependency design.
If module A exports let count = 0 and later updates that binding, module B that imported count observes the current exported binding rather than a snapshot captured at import time. The importer cannot assign directly to that imported binding.
Circular module graphs are therefore not handled by simply executing one complete file and copying its exports into the next. Modules are linked so bindings can refer to one another, then evaluated according to dependency rules. In a cycle, attempting to use a lexical export before it has been initialized can fail in a TDZ-like way.
- Live bindings help cycles exist without requiring every export to be copied eagerly.
- Cycles are still risky when top-level initialization depends on another module in the same cycle already having executed.
- A common design fix is to extract shared primitives or move work behind functions so evaluation-time dependencies are reduced.