Sub topic 8
Re-exporting JSON Data from Other Modules
This chapter assumes you know ES module import and export syntax, have read the earlier chapters on the with { type: "json" } clause and the default-only shape of JSON modules, and can run ES modules in Node.js 22 or later or a current browser.
Earlier chapters covered how a JSON module exposes one default export, and how a named import such as import { appName } from "./config.json" fails at link time. Re-exporting gets around that limit. An intermediate JavaScript module imports the JSON and gives the rest of the codebase a different interface: named exports, validated values, or a single "barrel" entry point. This chapter covers the re-export forms that work with JSON modules, where the import attribute must go, and how each form behaves at runtime.
The Running Example
All examples use this config.json:
{
"appName": "Inventory",
"version": 3,
"features": ["search", "export"]
}Direct Re-export with `export ... from`
The shortest form re-exports the JSON module's default export under a new name, in one statement:
// settings.js
export { default as config } from "./config.json" with { type: "json" };// main.js
import { config } from "./settings.js";
console.log(config.appName); // "Inventory"
console.log(config.version); // 3config is now a named export of settings.js, so main.js can use an ordinary named import. Neither the import nor the export creates a local binding in settings.js. The statement only forwards a binding from one module to another.
The attribute belongs to the statement that loads the JSON
Any export ... from declaration loads a module, the same way import does. So the declaration carries its own import attributes, and it needs with { type: "json" } for the same reason a static import does. If you leave the attribute off, the load fails:
// settings.js — broken
export { default as config } from "./config.json";
// Node.js: TypeError [ERR_IMPORT_ATTRIBUTE_MISSING]:
// Module ".../config.json" needs an import attribute of "type: json"The consumer, main.js, does not need an attribute. It imports a JavaScript module, and settings.js already declared the type at the place where the JSON gets loaded. Attributes don't spread down the import chain. Each edge that points directly at a JSON file carries its own attribute, and edges that point at JavaScript modules carry none.
Re-exporting as the default
You can also forward the default export unchanged:
// settings.js
export { default } from "./config.json" with { type: "json" };// main.js
import config from "./settings.js";
console.log(config.features); // ["search", "export"]This form helps when you want to move the JSON file, or replace it later with computed configuration, without changing any consumer.
Namespace Re-exports and the `export *` Trap
Two star forms look alike but behave very differently with a JSON module.
export * as name from creates a namespace object, and that object's default property holds the parsed JSON:
// settings.js
export * as configModule from "./config.json" with { type: "json" };// main.js
import { configModule } from "./settings.js";
console.log(configModule.default.appName); // "Inventory"This works. Consumers still have to write .default, though, which is the clumsy access you wanted to avoid in the first place.
A bare export * re-exports nothing useful:
// settings.js
export * from "./config.json" with { type: "json" };// main.js
import * as settings from "./settings.js";
console.log(Object.keys(settings)); // []The language defines export * to forward every named export except default. A JSON module has no export other than default, so the namespace ends up empty. The statement doesn't throw. The JSON file still gets fetched, checked, and parsed, and then nothing reaches the consumer. A later named import from settings.js fails with a link-time SyntaxError saying the requested export doesn't exist.
Import, Then Export: Building Named Exports
export ... from only forwards bindings. To expose individual properties as named exports, or to validate the data first, import the JSON into a local binding and export what you build from it:
// settings.js
import config from "./config.json" with { type: "json" };
if (typeof config.version !== "number") {
throw new TypeError("config.json: version must be a number");
}
export const appName = config.appName;
export const version = config.version;
export const features = Object.freeze([...config.features]);
export default config;// main.js
import settings, { appName, version, features } from "./settings.js";
console.log(appName, version); // Inventory 3
console.log(features.includes("search")); // true
console.log(settings === settings); // trueHere the top-level code in settings.js runs once, before any importer runs. A validation error therefore stops the whole module graph at startup, which is better than a confusing undefined showing up later. Freezing a copy of features stops consumers from changing that array. The earlier chapter on caching showed that the original JSON object is shared and mutable.
Snapshots versus live bindings
The two techniques differ in a way that can bite you.
- A direct re-export (
export { default as config } from ...) forwards a live binding. A JSON module never reassigns its default export, so in practice the binding always points at the same object, and every importer sees any change made to that object. export const appName = config.appNamecopies the value whensettings.jsevaluates. Primitives such as strings and numbers become independent snapshots. If some module later runsconfig.appName = "Other", the exportedappNamestays"Inventory".- Object and array properties copy only the reference.
export const list = config.featuresstill shares the original array, and apushfrom anywhere shows up everywhere. Copy the value, or copy and freeze it, if you need isolation.
// demo.js
import config from "./config.json" with { type: "json" };
import { appName } from "./settings.js";
config.appName = "Changed";
console.log(config.appName); // "Changed"
console.log(appName); // "Inventory": snapshot taken at evaluationChoosing a Form
| Form | Consumer writes | Exposes | Attribute needed in | Notes |
|---|---|---|---|---|
| `export { default as x } from "./f.json" with {...}` | `import { x }` | Whole object, live binding | Re-exporting module | Shortest way to get a named export |
| `export { default } from "./f.json" with {...}` | `import x` | Whole object | Re-exporting module | Hides the JSON file's location |
| `export * as ns from "./f.json" with {...}` | `ns.default.key` | Namespace object | Re-exporting module | Works, but awkward |
| `export * from "./f.json" with {...}` | n/a | Nothing | Re-exporting module | Silently empty |
| `import` then `export const` | `import { key }` | Chosen values | Re-exporting module | Allows validation, copies, and freezing |
Cache Identity Across Re-exports
Re-exporting doesn't create a second copy of the JSON. Hosts key their module map by the resolved URL together with the module type. Every path that reaches ./config.json as a JSON module gets the same instance, whether it comes through settings.js, through a barrel, or through a direct import elsewhere:
// check.js
import direct from "./config.json" with { type: "json" };
import { config } from "./settings.js"; // the direct re-export version
console.log(direct === config); // trueSo a re-export module doesn't protect the data. If you need a private copy for each consumer, export a function that returns a fresh structuredClone(config), or read the file yourself as described in the chapter on alternatives.
Barrel Files with JSON
A barrel file collects many modules behind one import path. With JSON, every entry needs an explicit default re-export and its own attribute:
// locales/index.js
export { default as en } from "./en.json" with { type: "json" };
export { default as fr } from "./fr.json" with { type: "json" };
export { default as de } from "./de.json" with { type: "json" };// app.js
import { en, fr } from "./locales/index.js";
console.log(en.greeting, fr.greeting);Static re-exports load every listed file as soon as the barrel is imported, including files the consumer never uses. A small, fixed set of files is fine. For many locales or large data files, use the dynamic import() approach from the earlier chapter, so each file is fetched only when someone asks for it.
Exercise
A teammate writes export * from "./flags.json" with { type: "json" }; in config/index.js. Then import { darkMode } from "./config/index.js" fails with a SyntaxError. Explain why, and give a one-line fix that lets consumers write import { flags } from "./config/index.js".
Show answer
export * forwards every named export except default, and a JSON module has only a default export. So the barrel exports nothing, and the named import fails at link time. Fix: export { default as flags } from "./flags.json" with { type: "json" };. Consumers then read flags.darkMode.
Exercise
settings.js contains import config from "./config.json" with { type: "json" }; export const version = config.version; export const features = config.features;. Another module runs config.version = 9; config.features.push("audit");. What do consumers of settings.js see for version and features?
Show answer
version stays 3, because a primitive was copied when settings.js evaluated. features shows ["search", "export", "audit"], because the export holds a reference to the same shared array. To isolate it, export Object.freeze([...config.features]) instead.
Exercise
main.js imports { config } from settings.js, which re-exports from config.json. Does main.js need with { type: "json" }? Which statement fails if the attribute is missing from settings.js?
Show answer
No. main.js imports a JavaScript module, so it needs no attribute. The export { default as config } from "./config.json" statement in settings.js loads the JSON, so it must carry the attribute. Without it, loading config.json fails, and the import graph of main.js fails along with it.