Sub topic 12
JSON Modules in Node.js
This chapter assumes you already know the with { type: "json" } syntax, the default-export-only rule, and the difference between ES modules and CommonJS in Node.js. It covers how Node.js decides a file is JSON, which versions support which syntax, and how JSON imports relate to require().
How Node.js Recognizes a JSON Module
Node.js picks a module's format from its file extension. A specifier ending in .json resolves to a JSON module, but Node.js still refuses to load it unless the import carries type: "json". The extension and the attribute must agree.
ES module specifiers in Node.js need the full file name. import config from "./config" will not find config.json, even though require("./config") would.
Version Support
| Node.js version | Behavior |
|---|---|
| 16.14 / 17.1 | JSON imports with `assert { type: "json" }`, experimental |
| 18.20 / 20.10 | `with { type: "json" }` accepted |
| 22.0 | `assert` removed; only `with` works |
| 22.12 / 23.1 and later | JSON modules stable, no experimental warning |
If an older runtime prints ExperimentalWarning: Importing JSON modules is an experimental feature, the import still works. The warning only tells you the feature was not yet stable in that release.
Errors You Will See
Node.js reports attribute problems with specific error codes:
| Code | Cause |
|---|---|
| `ERR_IMPORT_ATTRIBUTE_MISSING` | Importing a `.json` file without `type: "json"` |
| `ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE` | Using `type: "json"` on a file that is not JSON, such as a `.js` file |
| `ERR_IMPORT_ATTRIBUTE_UNSUPPORTED` | Passing an attribute key Node.js does not know, or a `type` value it does not support |
Invalid JSON content produces a plain SyntaxError that names the file.
JSON Imports and require()
CommonJS has always loaded JSON with require(), with no attribute needed. Inside an ES module you can still get that behavior through createRequire:
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
const config = require("./config.json");
console.log(config.appName);The two loaders keep separate caches. An object loaded through require() is a different object from the one loaded through import, so changing one does not affect the other. Within each loader, every importer of the same path shares a single object.
Resolving Paths from Packages
JSON files inside packages follow the package's exports map. Many packages do not export package.json, so import pkg from "some-lib/package.json" with { type: "json" } can fail with ERR_PACKAGE_PATH_NOT_EXPORTED. Your own package.json is always reachable through a relative path:
import pkg from "../package.json" with { type: "json" };
console.log(`${pkg.name}@${pkg.version}`);Exercise
On Node.js 22, a project's entry module contains import data from "./data" with { type: "json" }; and fails, even though data.json exists next to it. After the specifier is corrected, a second file that uses assert { type: "json" } also fails. Explain both failures and give the fixed lines.
Show answer
ES module resolution in Node.js does not add file extensions, so "./data" does not match data.json. The fix is import data from "./data.json" with { type: "json" };. Node.js 22 removed support for the assert keyword, so the second file must change assert to with: import x from "./x.json" with { type: "json" };.
Exercise
Module A runs const c = require("./config.json"); c.version = 9; after creating require with createRequire. Module B runs import config from "./config.json" with { type: "json" };. What does B see for config.version, assuming the file contains version 3?
Show answer
B sees 3. The CommonJS and ESM loaders keep separate caches, so A changed the object in the require cache and the ES module instance was not touched.