Sub topic 8
Re-exporting JSON Data from Other Modules
This chapter assumes you know static import declarations with with { type: "json" } and that a JSON module has only a default export. Here you will expose JSON data through a plain JavaScript module, so other code can import it without repeating attributes.
Why re-export JSON
Many projects keep several JSON files, such as configuration, feature flags and locale strings. If every file imports them directly, every import has to repeat the path and the with { type: "json" } clause. A small JavaScript module that re-exports the data gives you one place to change paths. It also lets you offer named exports, which a JSON module cannot provide.
The basic re-export
A re-export declaration takes import attributes in the same position as an import declaration:
// settings.js
export { default as config } from "./config.json" with { type: "json" };
// main.js
import { config } from "./settings.js";
console.log(config.version); // 3main.js imports from settings.js, which is ordinary JavaScript, so it needs no attributes. Only the declaration that names config.json needs them. If you leave the attribute off the re-export, the host rejects it the same way it rejects a plain import of JSON.
Re-export forms and what they produce
| Declaration | Result for importers |
|---|---|
| `export { default as config } from "./config.json" with { type: "json" };` | Named export `config` holding the parsed object |
| `export { default } from "./config.json" with { type: "json" };` | Default export holding the parsed object |
| `export * as config from "./config.json" with { type: "json" };` | Named export `config` holding the namespace, so the data is at `config.default` |
| `export * from "./config.json" with { type: "json" };` | Nothing, because `export *` skips the default export |
Exposing individual fields
To give importers real named exports for top-level fields, import the object and export from a destructuring declaration:
// settings.js
import config from "./config.json" with { type: "json" };
export const { appName, version, features } = config;
export default config;// main.js
import { appName, features } from "./settings.js";
console.log(appName, features.length); // "Inventory" 2The values are copied when settings.js runs. If code later sets config.version = 4, the exported version binding stays 3. Object values such as features still point at the same array, so changes to that array's contents show up everywhere. The export { default as config } form has no copy at all. It forwards the live binding to the one shared JSON object.
A barrel for several files
// data/index.js
export { default as config } from "./config.json" with { type: "json" };
export { default as flags } from "./flags.json" with { type: "json" };
export { default as en } from "./locales/en.json" with { type: "json" };Importing anything from data/index.js loads all three JSON files, because static re-exports become part of the module graph. If a file is large or only some users need it, load it with dynamic import() instead of listing it in the barrel.
Exercise
A module contains export * from "./config.json" with { type: "json" };. Another module writes import { appName } from "./settings.js";. What happens, and how do you fix it?
Show answer
Linking fails with a SyntaxError because settings.js has no export named appName. export * does not forward default exports, and a JSON module has nothing else. Fix it by importing the default and writing export const { appName } = config;, or by re-exporting { default as config } and reading config.appName.
Exercise
settings.js has export const { version } = config; and also export { config };. Another module runs config.version = 5. What do version and config.version show afterward?
Show answer
version is still 3, because it was copied when settings.js ran. config.version is 5, because every importer shares the same cached object.