Loading topic
import() and top-level await both change when modules load and run. Before looking at them, it helps to be clear about what happens by default. This chapter reviews how static import declarations work, how the runtime builds a module graph from them, and in what order that graph is evaluated. Th6 min readimport() and top-level await, and both depend on how the runtime treats your file. A browser or Node.js decides up front whether a file is an ES module or a classic script. That decision controls which syntax is legal, how the file is loaded, and which global helpers6 min readimport and export declarations, work with promises and async/await, and run a file as an ES module in a browser or in Node.js (for example, by giving it the .mjs extension). The examples use Node.js file names, but the behavior described here is the same i5 min readimport/export syntax, promises with async/await, and the basic import() call from the previous chapter. Every successful import() resolves to one value, the module namespace object. Nearly all the surprises with dynamic imports come fro4 min readimport/export syntax, promises with async/await, and the basic form of import() covered earlier: it takes a specifier and returns a promise for the module namespace object. The focus here is when to call it. Lazy loading means a module is fetched a5 min readimport that fails stops the whole module graph before any of your code runs. An import() call fails differently. It returns a promise, and that promise rejects. Your code keeps running, and you decide what happens next. This chapter covers where those failures come from, how to catch th5 min readimport() returns a promise for a module namespace object, how to destructure named and default exports from it, and how to catch a failed import with try/catch. It also assumes you are comfortable with Promise.all from general async/await work. The f5 min readimport/export syntax, async/await, and the basic import() call covered earlier. It looks at the string you pass to import(): what forms it can take, how the runtime turns it into a concrete file or URL, and what changes when you build that str5 min readimport() expression, module namespace objects, and how computed specifiers resolve. Those topics come from earlier chapters. You should also have built a project with a bundler such as Vite, Rollup, esbuild, or webpack at least once. The examples use Vite 6 min readimport/export syntax and promises with async/await. You should also know how to make a file run as a module in a browser or Node.js, which the earlier chapter on module contexts covered. The earlier chapters used import() inside async functions. Top-5 min readimport/export, Promises with async/await, and how to run ES modules in Node.js (.mjs files or "type": "module") or a browser (<script type="module">). It also assumes you met top-level await in the previous chapter. Here the focus is narrower: when 5 min readimport/export syntax, promises and async/await, and the import() expression. It also assumes you have seen how top-level await pauses a module and the modules that import it. The examples run in Node.js 18 or later with .mjs files or `"type4 min readimport() and import cycles, and places where top-l6 min readasync/await, and how Node.js decides whether a file is CommonJS or an ES module (the .cjs/.mjs extensions and the "type" field in package.json, covered earlier). It also assumes you know what a module namespace object is.5 min readimport() expression and its namespace object, try/catch with await, and how to run .mjs files in Node.js or serve modules to a browser. Earlier chapters showed that import() resolves to a namespace object and that a default export shows5 min read