Dodi’s price list imported as a module by this browser: what happens with no attribute, with the wrong MIME type, with the old assert, and when several…
Why it matters
A TypeError when importing JSON, assert suddenly becoming a SyntaxError, and a server sending the wrong type are the three most common dead ends. All three are clear once you know the attribute is a type request checked against the Content-Type, for safety.
The key idea
The type attribute is a contract: what arrives must be what was asked for
Common mistakes
Still writing assert { type: 'json' }assert is the old proposal’s form and has been removed from browsers and Node; it is now a SyntaxError. Change it to with { type: 'json' }; for a dynamic import() the option is { with: { type: 'json' } }.
Thinking the attribute is a formality you can skip With no attribute the browser asks for JavaScript, and refuses application/json. The attribute is a security gate: data cannot pose as code, because what is asked for as JSON is only ever parsed, never run.
The server answers text/plain or text/html for a .json file The attribute is right, but the Content-Type is not JSON, so the browser refuses. The fix is on the server: send Content-Type: application/json. Changing or dropping the attribute does not help.
Try it in this playground
See JSON refused with no attribute Import with no attribute, the server sending application/json.
Read the price Use with { type: 'json' } or import() with its options.
See code in disguise run The server sends JavaScript, the import has no attribute.
One object for everyone Add kasir.js, which changes the oil price.