Sub topic 7
One Instance per Module Graph: Caching and Mutation
This chapter assumes you know how static import declarations work and how a JSON module exposes its parsed value as the default export. It uses the config.json file from earlier chapters:
{
"appName": "Inventory",
"version": 3,
"features": ["search", "export"]
}The module map
The first time a module is requested, the runtime keeps a record of it in a module map. The map's key is the module's resolved URL together with its module type. Any later request for the same key gets the existing record back. The file is not read again, the JSON is not parsed again, and no new object is created.
Every module that imports ./config.json with type: "json" therefore gets the same JavaScript object. The runtime does not copy it per importer and does not freeze it.
Mutations are visible everywhere
A JSON module's object is an ordinary mutable object, so any importer can change it, and every other importer sees the change.
// a.js
import config from "./config.json" with { type: "json" };
config.version = 4;// b.js
import config from "./config.json" with { type: "json" };
console.log(config.version); // 4// main.js
import "./a.js";
import "./b.js";Running main.js prints 4. The runtime evaluates a.js first because main.js imports it first. By the time b.js runs, the shared object has already changed. If you swap the two imports in main.js, the output becomes 3. A bug like this depends on import order, which makes it hard to track down.
Protecting the shared object
Pick the approach that matches what you want:
| Goal | Technique | Effect |
|---|---|---|
| Nobody may change the config | Deep-freeze it in one wrapper module | Writes fail silently, or throw in strict mode, which modules always use |
| Each consumer gets its own scratch copy | `structuredClone(config)` | Changes stay local to that consumer |
| Share state on purpose | Mutate the import directly | Works, but make it explicit and documented |
A wrapper module keeps the freezing in one place:
// frozen-config.js
import raw from "./config.json" with { type: "json" };
function deepFreeze(value) {
for (const child of Object.values(value)) {
if (child !== null && typeof child === "object") deepFreeze(child);
}
return Object.freeze(value);
}
export default deepFreeze(raw);// consumer.js
import config from "./frozen-config.js";
config.features.push("print"); // TypeError: object is not extensibleFreezing changes the shared object itself. Any module that imports config.json directly also gets the frozen version once frozen-config.js has run.
Caching also means no reloads
The module map holds the record for as long as the page or process lives. If config.json changes on disk or on the server, importing it again returns the old value. Dynamic import() uses the same map, so it does not help here either.
Adding a query string such as ./config.json?v=2 produces a different URL and a new module instance. This works in browsers and in Node.js. Each instance stays in memory for good, though, so don't use this trick for frequent reloads. For data that changes at runtime, use fetch or a file read, which a later chapter covers.
Exercise
main.js imports b.js and then a.js, using the files above, with a.js setting config.version = 4. What does b.js log, and what does import("./config.json", { with: { type: "json" } }).then(m => console.log(m.default.version)) print if main.js runs it afterward?
Show answer
b.js logs 3, because it runs before a.js changes the object. The dynamic import then prints 4. It resolves to the same cached module map entry, and a.js has changed that shared object by then.
Exercise
Why does structuredClone(config) protect a consumer when const copy = config does not?
Show answer
const copy = config copies only the reference, so both names point to the shared object. structuredClone makes a deep, independent copy, so changes to it never reach the module map's object.