Import Attributes for Importing JSON the Standard Way
Import attributes add a with clause to import, and with type json you load a JSON file as a module with one default export. See the syntax and errors.

Loading a JSON file in Node.js used to mean a choice between require("./config.json"), which does not exist in ES modules, and reading the file with fs and calling JSON.parse. Both work and neither is a standard import. Import attributes finish the job: a with clause on an import tells the loader what kind of thing the file is, and type: "json" asks for a JSON module. The syntax is small and its reason is security, because a loader that treated any file as the kind its URL suggested could be tricked into running data as code. This post shows the static and dynamic forms, the errors you will meet, how the module behaves once loaded, and when to choose another route. Output is from Node.js 24 as an ES module, and the JSON files are served from data: URLs so the examples need no extra files. The module system behind it is in the post on ES modules, and the dynamic form is in the post on dynamic import().
The syntax
A static import adds a with clause after the specifier. The imported value is the parsed JSON, bound to a default import.
// config.json
// { "name": "app", "port": 8080, "features": ["a", "b"] }
import config from "./config.json" with { type: "json" };
console.log(config.port);
console.log(config.features.length);The same works for a re-export and for the dynamic form:
export { default as config } from "./config.json" with { type: "json" };
const loaded = await import("./config.json", { with: { type: "json" } });
console.log(loaded.default.name);The dynamic form takes the attributes in a second argument shaped as { with: { type: "json" } }. In both forms the attribute is part of the module's identity, so the same URL loaded with different attributes is a different module request. Every example below uses the dynamic form, because it can load from a data: URL in a single file.
const json = (text) => "data:application/json," + encodeURIComponent(text);
const config = await import(json('{"name":"app","port":8080,"features":["a","b"]}'), {
with: { type: "json" },
});
console.log(Object.keys(config), config.default);
console.log(config.default.port, config.default.features.length);
console.log(typeof config.port, config[Symbol.toStringTag]);[ 'default' ] { name: 'app', port: 8080, features: [ 'a', 'b' ] }
8080 2
undefined ModuleThe namespace has one key, default, holding the parsed value, and no property for each field of the JSON. That is the rule: a JSON module exports a single value, so import { port } from "./config.json" with { type: "json" } is a link error. Destructure after importing instead, with const { port } = config;.
The attribute is required
Without the attribute, Node.js refuses to load the file as JSON. The error names the missing attribute, which is the fastest way to recognize the problem.
const json = (text) => "data:application/json," + encodeURIComponent(text);
const source = json('{"ok":true}');
const attempts = [
["no attributes", () => import(source)],
["wrong value", () => import(source, { with: { type: "css" } })],
["unknown key", () => import(source, { with: { format: "json" } })],
["correct", () => import(source, { with: { type: "json" } })],
];
for (const [label, load] of attempts) {
try {
const mod = await load();
console.log(label.padEnd(14), "loaded", JSON.stringify(mod.default));
} catch (error) {
console.log(label.padEnd(14), error.code ?? error.name);
}
}no attributes ERR_IMPORT_ATTRIBUTE_MISSING
wrong value ERR_IMPORT_ATTRIBUTE_UNSUPPORTED
unknown key ERR_IMPORT_ATTRIBUTE_MISSING
correct loaded {"ok":true}The loader needs type: "json" to treat the data as JSON. An unsupported value is reported as ERR_IMPORT_ATTRIBUTE_UNSUPPORTED, and an unknown key is ignored, which leaves the required attribute missing, so it ends in ERR_IMPORT_ATTRIBUTE_MISSING again. Nothing is guessed. The requirement protects the default assumption: a URL ending in .json is not trusted to be data merely because of its name, and a server that returned JavaScript with a JSON content type cannot slip code through a module that asked for data.
Invalid JSON and mutation
The file is parsed when it loads. A syntax error rejects the import with a SyntaxError, and the parsed value is shared by every importer.
const json = (text) => "data:application/json," + encodeURIComponent(text);
try {
await import(json("{ not valid json }"), { with: { type: "json" } });
} catch (error) {
console.log(error.name);
}
const source = json('{"visits":0,"tags":[]}');
const first = await import(source, { with: { type: "json" } });
const second = await import(source, { with: { type: "json" } });
console.log(first === second, first.default === second.default);
first.default.visits += 1;
first.default.tags.push("shared");
console.log(second.default);
Object.freeze(first.default);
try {
first.default.visits = 99;
} catch (error) {
console.log(error.name);
}SyntaxError
true true
{ visits: 1, tags: [ 'shared' ] }
TypeErrorBoth imports returned the same module and the same object, so a change made through one name showed in the other. Treat the default export of a JSON module as shared, read-only configuration, and copy it with structuredClone() if you need your own mutable version, as described in the post on structuredClone(). The freeze made later writes throw in strict mode, a good habit for configuration that nothing should edit.
What it replaces
Three older routes load JSON, and each has a role left.
import { readFile } from "node:fs/promises";
import { createRequire } from "node:module";
const text = '{"name":"app","port":8080}';
const viaParse = JSON.parse(text);
const require = createRequire(import.meta.url);
console.log(typeof require, typeof require.resolve);
const bytes = await readFile(new URL(import.meta.url));
console.log(bytes.length > 0);
console.log(viaParse.port);function function
true
8080Reading and parsing is the right choice when the data changes while the program runs, when the path is computed from user input, or when you want to validate the content before use. createRequire gives an ES module a CommonJS require, which can load JSON without attributes and is common in code migrated from CommonJS, though it uses a different cache. The import attribute is the choice for fixed data that ships with the code, such as a manifest or default settings, where static analysis and bundling help.
A fourth item, the assert keyword, is no longer an option.
const json = (text) => "data:application/json," + encodeURIComponent(text);
try {
await import(json("{}"), { assert: { type: "json" } });
console.log("old keyword accepted");
} catch (error) {
console.log(error.code ?? error.name);
}
try {
eval(`import("data:application/json,%7B%7D", { with: { type: "json" } })`);
console.log("with accepted");
} catch (error) {
console.log(error.name);
}ERR_IMPORT_ATTRIBUTE_MISSING
with acceptedThe first proposal used assert, and it was changed to with when the semantics changed from checking to influencing the load. The run above shows the effect: the old keyword is no longer read, so the required attribute is reported as missing. Node.js 22 removed support for assert, so code written for earlier versions needs a mechanical rename. Tools such as TypeScript and bundlers accept with in current versions.
TypeScript and bundlers
TypeScript can type a JSON module if resolveJsonModule is enabled, and with module set to nodenext or node16 it requires the with { type: "json" } clause on imports that Node.js would load as ES modules. The compiler infers the type of the default export from the file's content, so config.port is a number, and a typo in a field name is a compile error. Bundlers such as Vite and esbuild load JSON natively and may or may not require the attribute, so keep it in the source: it is what Node.js and browsers need.
Browsers support the same syntax, and the same attribute extends to other types. A CSS module, with { type: "css" }, imports a stylesheet as a CSSStyleSheet in browsers that support it, and Node.js does not load CSS, as the unsupported-type error above showed.
A small test for a configuration module
A module that loads JSON and exposes validated settings is a good place for a test, because the failures are quiet: a renamed key or a wrong type reaches production as undefined. Load the data through the same path the application uses and assert the shape.
import { test } from "node:test";
import assert from "node:assert/strict";
const json = (text) => "data:application/json," + encodeURIComponent(text);
async function loadSettings(text) {
const { default: raw } = await import(json(text), { with: { type: "json" } });
if (!Number.isInteger(raw.port)) throw new TypeError("port must be an integer");
return Object.freeze({ port: raw.port, features: [...(raw.features ?? [])] });
}
test("valid settings load and are frozen", async () => {
const settings = await loadSettings('{"port":8080,"features":["a"]}');
assert.equal(settings.port, 8080);
assert.throws(() => {
settings.port = 1;
}, TypeError);
});
test("a bad port is rejected at load time", async () => {
await assert.rejects(loadSettings('{"port":"8080"}'), TypeError);
});Run it with node --test on Node.js 24. The function copies the fields it needs into a frozen object, so the shared module value is never exposed for mutation, and the second test shows the string port failing at startup and not on the first request.
Where JSON modules stop being the right tool
Do not use them for large data files that only some code paths need, since a static import loads the file at startup. Use a dynamic import() or a lazy read. Do not use them for secrets: a file imported into the module graph is bundled into client code. Do not rely on them to reload data, because the module is cached for the life of the process, and a changed file is not seen until restart. And validate anything that did not come from your own repository, because JSON that parses is not JSON that has the shape your code expects.
The feature is specified in the import attributes proposal, which is part of ES2025, and MDN documents the syntax on its import attributes page.


