Sub topic 5
JSON Modules Have Only a Default Export
This chapter assumes you know ES module import and export syntax and how the with { type: "json" } clause works in a static import. It covers what a JSON module exports and why some import forms that look reasonable fail.
The Whole Document Is the Default Export
When the runtime loads a JSON module, it parses the file with the same rules as JSON.parse and makes the result the module's default export. The module has no other exports.
Take this config.json:
{
"appName": "Inventory",
"version": 3,
"features": ["search", "export"]
}A default import gives you the parsed object:
import config from "./config.json" with { type: "json" };
console.log(config.appName); // "Inventory"
console.log(config.features[1]); // "export"The name config is your choice, as with any default import.
Why Named Imports Fail
Developers often try to import top-level keys directly, the way they would from a JavaScript module:
import { appName } from "./config.json" with { type: "json" };This fails at link time, before any of your code runs. The engine checks every requested name against the module's export list. appName is not on that list, so the engine throws a SyntaxError. In Chromium and Node.js the message reads roughly: The requested module './config.json' does not provide an export named 'appName'.
The specification exposes only a default export for these reasons:
- The top level may not be an object. A JSON file can hold an array, a string, a number,
true, ornull. None of these has keys that could become export names. - Keys may not be valid identifiers.
"app-name","2fa", and""are legal JSON keys. Mapping them to exports would need special rules. - One rule is simpler. If the whole value is always the default export, every JSON file behaves the same way, whatever its shape.
Working Patterns
Destructure After Importing
To get short local names, import the default and destructure it:
import config from "./config.json" with { type: "json" };
const { appName, version } = config;
console.log(appName, version); // "Inventory" 3Namespace Imports
A namespace import works, but the data sits under default:
import * as configModule from "./config.json" with { type: "json" };
console.log(configModule.default.appName); // "Inventory"
console.log(configModule.appName); // undefinedThe second line does not throw, because reading a missing property on a namespace object returns undefined. Bugs like this can go unnoticed.
Explicit Default Syntax
import { default as config } is the same as import config and also works. It rarely adds anything, but you will see it in re-exports.
Summary of Import Forms
| Import form | Result |
|---|---|
| `import config from "./x.json" with { type: "json" }` | Works, `config` is the parsed value |
| `import { default as config } from ...` | Works, same as above |
| `import * as ns from ...` | Works, data is at `ns.default` |
| `import { appName } from ...` | `SyntaxError` at link time |
Exercise
A file colors.json contains ["red", "green", "blue"]. Write an import that logs "green", and explain why import { 1 as second } from "./colors.json" with { type: "json" } could never work.
Show answer
Use import colors from "./colors.json" with { type: "json" }; console.log(colors[1]);. The named form fails for two reasons. JSON modules export only default, so no export named 1 exists. The top-level value is also an array, which has no keys that could serve as export names.
Exercise
A teammate writes import * as data from "./config.json" with { type: "json" }; if (!data.version) throw new Error("missing version");. The error fires even though the file has "version": 3. Why, and what is the fix?
Show answer
A namespace object holds only the module's exports, and the only export here is default. data.version is therefore undefined. Read data.default.version instead, or switch to a default import: import data from "./config.json" with { type: "json" };.