JavaScript · Modules · Mid-Level · Concept
How many times is an ES module evaluated, and how does that affect singletons?
Short Interview Answer
Each module is evaluated once per module map/realm; subsequent imports get the same exports — module scope is a natural singleton.
Detailed Explanation
The module graph deduplicates by resolved URL (and attributes). Side effects in module scope run once, which is useful for registering singletons and dangerous for unintended side effects. Hot Module Replacement and multiple bundles/copies of the same library can create multiple evaluations — classic 'duplicate React' issues. Dynamic `import()` still returns the same module namespace after first load. Design modules to be idempotent when possible.
Example
// auth.js
export const store = { user: null }; // shared across importersInterview Tip
Mention duplicate-library problems in bundlers.
Common Mistake
Assuming importing a module twice re-runs top-level initialization.