Sub topic 3
Import Attributes: The with { type: "json" } Clause
This chapter assumes you already know ES module import and export statements and JSON syntax. It covers the grammar of import attributes: where the clause goes, what it may contain, and which forms the parser rejects.
The Shape of the Clause
An import attribute is a key-value pair that tells the host (the browser or Node.js) how to treat the module you are requesting. You write the attributes in a with clause directly after the module specifier, before the semicolon.
import config from "./config.json" with { type: "json" };
console.log(config.appName); // "Inventory"The clause looks like an object literal, but it is fixed syntax, not an expression. You cannot pass a variable to it or build it at runtime.
Grammar Rules
The parser checks the attribute list against these rules:
| Rule | Allowed | Rejected |
|---|---|---|
| Key form | Identifier `type` or string `"type"` | Computed key `[k]` |
| Value form | String literal `"json"` | Variable, template literal, number |
| Duplicate keys | — | `{ type: "json", type: "json" }` (SyntaxError) |
| Trailing comma | `{ type: "json", }` | — |
| Empty list | `with {}` | — |
An empty with {} is legal, but it does not import a JSON file. Without type: "json" the host treats the file as JavaScript and the import fails. The next chapter explains why.
Every Form That Accepts the Clause
The clause works on every static form that names a module specifier:
// Default binding
import config from "./config.json" with { type: "json" };
// Namespace binding: the data lives on ns.default
import * as ns from "./config.json" with { type: "json" };
// Side-effect-only import: loads and caches, binds nothing
import "./config.json" with { type: "json" };
// Re-export (covered in its own chapter)
export { default as settings } from "./config.json" with { type: "json" };
console.log(config === ns.default); // trueDynamic import() takes the same attributes as a second argument, { with: { type: "json" } }. A later chapter covers it.
Attributes Are Part of the Module's Identity
The host uses the attributes, together with the resolved URL, to look up modules in its module map. So the attributes are part of the request itself. They are not hints the host can ignore. This has two practical effects:
- Every import of the same JSON file must carry
type: "json". You cannot rely on another module having imported it first. - A host must reject attribute keys it does not support. Today the only standard key is
type, so a typo such as{ tpye: "json" }fails before any code runs.
Common Syntax Mistakes
// Wrong: value must be a string literal
// const t = "json";
// import config from "./config.json" with { type: t };
// Wrong: the older "assert" keyword (see the migration chapter)
// import config from "./config.json" assert { type: "json" };
// Right
import config from "./config.json" with { type: "json" };Exercise
Which of these declarations parse successfully, and which of those actually load config.json as JSON? (1) import c from "./config.json" with { "type": "json", }; (2) import c from "./config.json" with {}; (3) import c from "./config.json" with { type: json };
Show answer
(1) parses and loads JSON: a string-literal key and a trailing comma are both allowed. (2) parses, but it requests a JavaScript module, so the import fails when the host loads the file. (3) is a SyntaxError because attribute values must be plain string literals, not template literals.
Exercise
Module a.js runs import "./data.json" with { type: "json" }; and module b.js runs import d from "./data.json"; in the same app. Does b.js get the parsed JSON?
Show answer
No. The host keys modules by URL and attributes together, so b.js makes a separate request for a JavaScript module. That request fails because the file is JSON, not JavaScript.