Sub topic 9
When Imports Fail: Common Errors and Their Causes
This chapter assumes you already know the with { type: "json" } syntax and dynamic import() from earlier chapters, and that you can run code in Node.js or a browser.
JSON module imports fail in four common ways. The error you get depends on which check failed and where the code runs. Once you know the order of those checks, the error messages make sense.
Where the checks happen
The host is the browser or Node.js. It checks an import in a fixed order. First it checks the attributes against what it supports. Then it fetches the file and compares the file's type to the type attribute. Last, it parses the file's text as JSON.
The four failure modes
| Error | Typical cause | Fix |
|---|---|---|
| Missing type | `import config from "./config.json"` with no attribute | Add `with { type: "json" }` |
| Bad MIME type | Server sends `text/plain` or `text/html` for the file | Serve it as `application/json` |
| Invalid JSON | Trailing comma, comments, or single quotes in the file | Fix the file so it is strict JSON |
| Unsupported attribute | A misspelled key or an unknown `type` value, such as `{ typ: "json" }` | Use only `type`, with a value the host supports |
Missing type
If you leave out the attribute, the host treats the file as JavaScript and refuses it. Node.js throws TypeError [ERR_IMPORT_ATTRIBUTE_MISSING], and the message tells you the module "needs an import attribute of type: json". A browser reports that it expected a JavaScript module script but the server sent application/json.
Bad MIME type
Browsers never use the file extension to decide the type. They go by the Content-Type header the server sends. If a misconfigured server sends text/html, perhaps from a 404 page, the import fails even though your syntax is correct. Look at the response headers in the Network panel.
Invalid JSON
The file is parsed with strict JSON rules, so { "a": 1, } is a SyntaxError. Files that allow comments, such as JSONC, are not valid JSON either.
Unsupported attributes
Hosts reject attribute keys and values they don't understand. They do not silently ignore them. Node.js uses the code ERR_IMPORT_ATTRIBUTE_UNSUPPORTED. If you put type: "json" on a file that is really JavaScript, Node.js throws ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE.
Static versus dynamic failures
This Node.js example falls back to a default locale and logs the error code:
async function loadLocale(lang) {
try {
const mod = await import(`./locales/${lang}.json`, {
with: { type: "json" }
});
return mod.default;
} catch (err) {
console.error(`Could not load ${lang}:`, err.code ?? err.name, err.message);
if (lang !== "en") return loadLocale("en");
throw err;
}
}
const messages = await loadLocale("fr");
console.log(messages);If fr.json does not exist, err.code is ERR_MODULE_NOT_FOUND. If the file has a trailing comma, err.name is SyntaxError.
Exercise
In a browser, import data from "/api/data.json" with { type: "json" }; fails with a message saying a JSON module was expected but the server sent "text/html". The file is valid JSON. What is the most likely cause, and how do you confirm it?
Show answer
The server is not returning the JSON file. The most likely cause is a 404 or a fallback HTML page, such as a single-page-app catch-all route, or a server that maps the path to HTML. Open the request in the Network panel and check the status code and the Content-Type header. Fix the path or the server configuration so the file is served with application/json.
Exercise
Why can't you wrap a static JSON import in try...catch to handle invalid JSON?
Show answer
Static imports are resolved, fetched, and parsed before any code in the module graph runs. A parse failure stops the whole graph from evaluating, so a try...catch never runs. Use dynamic import() inside try...catch instead.