Sub topic 16
Circular Dependencies and How to Untangle Them
This chapter assumes you know the module lifecycle from the previous chapter: modules are parsed, linked, then evaluated depth-first. It also assumes you know how let and const behave before their declaration runs.
What a Cycle Looks Like
A circular dependency is a loop in the import graph: a.js imports b.js, and b.js imports a.js, directly or through other modules. ES modules allow cycles. Linking succeeds because bindings are connected by name before any code runs. Problems appear only at evaluation time.
// a.js
import { b } from './b.js';
export const a = 'A';
console.log(b);
// b.js
import { a } from './a.js';
export const b = 'B';
console.log(a); // ReferenceError when a.js is the entry point: a is not yet initializedRun a.js as the entry point and you get ReferenceError: Cannot access 'a' before initialization.
Why Evaluation Order Causes the Error
The engine evaluates dependencies before the module that imports them. When it starts at a.js, it first walks into b.js. When b.js imports a.js, the engine sees that a.js is already being visited, so it does not loop forever. It evaluates b.js right away instead. At that moment the body of a.js has not run, so the binding a exists but is still in its temporal dead zone (TDZ). The TDZ is the period during which a let, const, or class binding exists but cannot be read.
If you start from b.js instead, the roles swap and a.js fails reading b. The error depends on which module the program enters first, which makes cycles hard to debug.
Ways to Untangle a Cycle
| Strategy | When it fits |
|---|---|
| Move shared code into a third module | Both files need the same values |
| Read imports inside functions, not at top level | The cycle is real but the access can wait |
| Use function declarations for exported helpers | Callers need a function before the exporter runs |
Extracting shared code is usually the cleanest fix:
// constants.js
export const a = 'A';
export const b = 'B';
// a.js
import { b } from './constants.js';
console.log(b);
// b.js
import { a } from './constants.js';
console.log(a);Deferring access also works, because by the time a function is called, every module in the cycle has finished evaluating:
// b.js
import { a } from './a.js';
export const b = 'B';
export function showA() {
console.log(a); // safe if called after a.js has evaluated
}Exercises
Exercise
In the original a.js/b.js example, which module throws, and what error do you get, if you run b.js as the entry point?
Show answer
a.js throws. The engine evaluates a.js first as b.js's dependency, and console.log(b) in a.js reads b while it is still in the TDZ, giving "ReferenceError: Cannot access 'b' before initialization".
Exercise
A module exports export const format = (x) => String(x); and is part of a cycle. Calls to format fail with a ReferenceError. What is the smallest change that makes the calls work, assuming callers invoke format during evaluation?
Show answer
Change it to a function declaration: export function format(x) { return String(x); }. Function declarations are initialized during linking, so the binding can be used before the module body runs.