Sub topic 4
Why the Type Attribute Is Required
This chapter assumes you know how ES modules are fetched and linked, and that you have seen the basic with { type: "json" } syntax from the previous chapter. The question here is why JavaScript makes you write the attribute at all, when a file ending in .json already looks like it says what it is.
The browser does not trust file extensions
On the web, a file extension tells the browser almost nothing. A browser decides how to treat a response from its Content-Type header, also called the MIME type, which the server sets. A URL like https://cdn.example.com/data.json could return JavaScript if the server sends Content-Type: text/javascript.
Suppose JSON modules worked without an attribute, and the browser picked the module type from the MIME type alone:
// Hypothetical: no attribute, type inferred from the server's response
import data from "https://cdn.example.com/data.json";You wrote this line expecting data. Parsing data cannot run code. If the third-party server is compromised, or simply misconfigured, it can serve JavaScript at that URL instead. The browser would then execute that script with full access to your page: cookies, the DOM, and every API your origin can call. A line that looked like a harmless data import would have turned into running someone else's code.
The attribute states what you expect
The type attribute moves the decision from the server to the importer. You declare the type you expect, and the host checks the response against it:
import data from "https://cdn.example.com/data.json" with { type: "json" };The browser now fetches the file and requires a JSON MIME type, such as application/json. If the server responds with text/javascript, the import fails and nothing runs. The worst a hostile server can do is make your import fail.
The check works in the other direction too. Without an attribute, the browser treats the import as JavaScript, and it rejects a response served as application/json. Each module type has to be requested explicitly.
Why the rule also applies outside browsers
Node.js reads local files and looks at file extensions, so the attack above does not really apply there. Node still requires the attribute and throws ERR_IMPORT_ATTRIBUTE_MISSING when you leave it out. The reason is portability: one module graph should behave the same way in every runtime. If code that imports JSON without an attribute worked in Node, it would break when moved to a browser or Deno.
| Situation | Browser result | Node.js result |
|---|---|---|
| `with { type: "json" }`, JSON served or found | Parsed default export | Parsed default export |
| No attribute, JSON file | MIME type error | `ERR_IMPORT_ATTRIBUTE_MISSING` |
| `with { type: "json" }`, server sends JavaScript | Import fails, script not run | Not applicable (local file) |
Key points
- Browsers choose how to treat a module from its MIME type, which the server controls.
- Without an explicit type, a "data" import could be swapped for executable code.
type: "json"makes the importer state its expectation, and a mismatch fails without running code.- Runtimes that do not need the check, such as Node.js, still enforce it so that code stays portable.
Exercise
Your page has import prices from "https://partner.example/prices.json" with { type: "json" };. The partner's server is compromised, and it starts serving alert(document.cookie) with Content-Type: text/javascript. What happens, and what would happen if JSON modules inferred their type from the response alone?
Show answer
With the attribute, the browser sees that the JavaScript MIME type does not match the declared JSON type, so the import fails and the script never runs. If the type were inferred from the response, the browser would evaluate the response as a JavaScript module and run the attacker's code with your page's privileges.
Exercise
Node.js resolves local files by extension, so a malicious MIME type cannot trick it. Why does Node still reject import config from "./config.json";?
Show answer
Node enforces the attribute so that module code behaves the same in every runtime. Code that worked in Node without the attribute would fail in browsers, so Node throws ERRIMPORTATTRIBUTE_MISSING to keep the rule consistent.