Sub topic 2
What a JSON Module Is
This chapter assumes you know ES module import and export, JSON syntax and JSON.parse, and how to run JavaScript in a browser or Node.js. Here you will build a working model of what the engine creates when a JSON file takes part in a module graph. Later chapters cover the attribute syntax and the reasons behind its rules.
A JSON file treated as a module
A JSON module is a module whose source text is a JSON document instead of JavaScript. The host (the browser or Node.js) loads the file, parses it as JSON, and wraps the result in a module record. Other modules can then import that record, just as they import JavaScript modules.
Take this file:
{
"appName": "Inventory",
"version": 3,
"features": ["search", "export"]
}A JavaScript module can import it directly:
import config from "./config.json" with { type: "json" };
console.log(config.appName); // "Inventory"
console.log(config.features[0]); // "search"The with { type: "json" } clause tells the host to treat the file as JSON. The next chapter explains that clause.
What the engine builds
A JavaScript module has code that runs, along with declarations, imports, and exports. A JSON module has none of these. The host builds what the specification calls a synthetic module record. This is a module record created by the host, not compiled from JavaScript source. It has these properties:
| Property | JavaScript module | JSON module |
|---|---|---|
| Source parsed as | JavaScript | JSON, with `JSON.parse` rules |
| Runs code during evaluation | Yes | No |
| Can import other modules | Yes | No, it is always a leaf |
| Exports | Whatever it declares | Exactly one: `default` |
| Evaluated | Once per module graph | Once per module graph |
The value of the default export is the parsed JSON value. Usually that is an object or array, but any valid JSON top-level value works. A file containing only 42 or "hello" gives a module whose default export is that number or string.
Consequences of this model
Several practical facts follow from the table above.
- Parsing is strict. The file must be valid JSON. Comments, trailing commas, and single quotes cause a parse error, and that error makes the whole import fail.
- Nothing executes. A JSON module cannot run code, so loading one has no side effects beyond creating the data.
- It is a leaf node. JSON cannot contain
importstatements, so a JSON module never pulls in further dependencies. - It loads with the graph. A static JSON import is fetched and parsed before any importing code runs. Your module can use the data at top level without
await. - There is one default export. The keys of the object are not separate exports. The "Default Export Only" chapter covers what this means for named imports.
Where JSON modules fit
JSON modules suit static data that ships with your code: configuration, translation strings, lookup tables, and fixtures. They are a poor fit for data that changes while the program runs. The module is evaluated once and then cached, as a later chapter shows.
Exercise
A file limits.json contains only [10, 20, 30]. What is the default export of the JSON module, and how many other exports does the module have?
Show answer
The default export is the array [10, 20, 30]. The module has no other exports. A JSON module always exposes exactly one export, default, whatever top-level JSON value the file contains.
Exercise
You add a comment line // production settings to the top of config.json. What happens when a module imports it with type: "json", and why?
Show answer
The import fails with a parse error, and the importing module does not run. JSON modules use strict JSON parsing rules, and comments are not valid JSON.