Sub topic 11
JSON Modules in the Browser: MIME Types and Servers
This chapter assumes you know how to write ES module imports, how to serve files from a local web server, and how to read the Network panel in browser developer tools. It covers what the browser checks when it loads a JSON module and how to set up a server so those checks pass.
What the Browser Checks
When the browser reaches import config from "./config.json" with { type: "json" }, it fetches the URL just as it fetches any other module. The type attribute only tells the browser what kind of module you expect. The server's response has to back that up. The browser accepts the file as a JSON module only when the response's Content-Type header is a JSON MIME type, such as application/json, text/json, or a type ending in +json like application/ld+json.
The checks happen in this order:
- The request must be allowed. Module requests use CORS, so a cross-origin JSON file needs an
Access-Control-Allow-Originheader. - The response status must indicate success (2xx).
- The MIME type must be a JSON MIME type.
- The body must parse as valid JSON.
If any check fails, the whole module graph fails to load. Your importing script never runs.
A Page That Uses a JSON Module
A JSON import only works inside a module script, so the page needs type="module":
<!DOCTYPE html>
<html>
<body>
<script type="module">
import config from "./config.json" with { type: "json" };
document.body.textContent = `${config.appName} v${config.version}`;
</script>
</body>
</html>If you open this file directly with a file:// URL, it fails. Browsers block module scripts loaded from file:// because those requests have no usable origin for CORS. Serve the page over HTTP instead.
A Minimal Server with Correct MIME Types
Most static servers already send application/json for .json files. If yours doesn't, or if you write your own, set the header explicitly. This Node.js server serves index.html and config.json from the current folder:
// server.mjs — run with: node server.mjs
import { createServer } from "node:http";
import { readFile } from "node:fs/promises";
import { extname, join } from "node:path";
const types = {
".html": "text/html; charset=utf-8",
".js": "text/javascript; charset=utf-8",
".json": "application/json; charset=utf-8"
};
createServer(async (req, res) => {
const path = req.url === "/" ? "/index.html" : req.url;
try {
const body = await readFile(join(process.cwd(), path));
res.writeHead(200, {
"Content-Type": types[extname(path)] ?? "application/octet-stream"
});
res.end(body);
} catch {
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("Not found");
}
}).listen(8080, () => console.log("http://localhost:8080"));This server is for local testing only. It does not sanitize paths.
Browser Support
| Browser | `with` syntax supported from |
|---|---|
| Chrome / Edge | 123 |
| Safari | 17.2 |
| Firefox | 138 |
Older Chromium versions (91 to 122) supported only the legacy assert keyword. Import maps also work with JSON modules: a bare specifier such as "app-config" can map to a JSON URL, and the browser runs the same MIME check on whatever URL it resolves to.
Exercise
A page imports https://cdn.example.com/data.json with { type: "json" }. The Network panel shows status 200 and Content-Type: application/json, but the import still fails. What is the most likely cause, and how do you fix it?
Show answer
The file is cross-origin, and module requests use CORS. The CDN is probably not sending an Access-Control-Allow-Origin header that permits your page's origin. Configure the CDN to send that header, or serve the file from your own origin.
Exercise
You serve a JSON file with Content-Type: application/geo+json. Will a JSON module import accept it?
Show answer
Yes. Any MIME type whose subtype ends in +json counts as a JSON MIME type, so the browser accepts it.