Loading topic
import and export, how JSON.parse turns text into values, and how to run a module script in a browser or Node.js. The previous chapter covered how the module loader fetches, links, and evaluates a graph of modules. This chapter applies that mode5 min readimport and export, and that you can run a module in a browser or in Node.js. The previous chapter explained what a JSON module is. This one covers the syntax that asks for one: the import attributes clause. You will see where the clause ca6 min readwith { type: "json" } clause is written. Those topics were covered in the earlier chapters. Here we look at why the language makes you write that clause at all, and what goes wrong without it.6 min readimport and export syntax, have seen the with { type: "json" } clause from the previous chapter, and can run module code in Node.js (version 22 or later) or a modern browser. Every example imports this config.json file:5 min readimport declaration loads its JSON before any of your code runs. Sometimes the module to load is only known at runtime: a locale picked by the user, a feature flag file, or a dataset that is too large to fetch on every page load. Dynamic import() covers these cases, and it accepts import6 min readimport and export, can run JavaScript in Node.js or a browser, and have read the earlier chapters on the with { type: "json" } clause and the default-only export of JSON modules. You will learn what the runtime does after it parses a JSON file once, why 5 min readimport and export syntax, have read the earlier chapters on the with { type: "json" } clause and the default-only shape of JSON modules, and can run ES modules in Node.js 22 or later or a current browser.5 min readimport with with { type: "json" }, and that a JSON module exposes a single default export. It also assumes you can run code in Node.js or a browser and read a stack trace. The goal here is diagnosis. When a JSON import fails, the error mes6 min readwith { type: "json" } syntax and why the type attribute is required. Older codebases, blog posts, and Stack Overflow answers often show a different form, assert { type: "json" }. That form is called import assertions. It was an earlier version of the5 min readimport ... with { type: "json" } declaration and a dynamic import() call with an options object. You also need to be able to serve files over HTTP from a local server. Here the focus moves from syntax to the network: what a browser checks when 6 min readimport/export, how to run .mjs files or set "type": "module" in package.json, and the with { type: "json" } syntax covered earlier. It covers what is specific to Node.js: how Node decides a file is JSON, which versions support what, the error codes5 min readasync/await, fetch, and the Node.js fs module. The goal here is t6 min readtype attribute is not only for JSON. Import attributes give the module loader a general way to answer "what kind of resource is this, and how should I turn it into a module?" JSON was the first answer. This chapter covers the second one that browsers ship, CSS module scripts. It also looks at 6 min read