JavaScript · Senior · Question 35 of 75
What can happen with a circular dependency between ES modules?
// a.js
import { b } from "./b.js";
export const a = "A" + b;
// b.js
import { a } from "./a.js";
export const b = "B" + a;- ✓Correct answer:
ReferenceError: accessing aconstbefore initialization - 2The bundler automatically merges the modules into one and it works
- 3Both values become
undefined, but there's no error
Explanation
Modules execute in graph order; with a cycle, one of them starts running while the other's export is still in the TDZ. With var you'd get undefined; with const/let/classes, an error. The fix is to move shared code into a third module or access the import lazily inside functions.