Browser import maps
How it works
Browsers resolve relative and absolute URL specifiers, while bare names normally require mapping. An import map is JSON embedded in a script type="importmap" element. Its imports object can map a bare specifier to a URL; scopes can provide mappings based on the importing module location. Register the mapping before requesting the module that needs it. Run these files through a local HTTP server, not as arbitrary console snippets.
Example 1
Save each file separately. Serve with a local HTTP server and open index.html. This is a multi-file example, not a single editor snippet.
index.html
<!doctype html><html lang="en"> <meta charset="utf-8"> <title>Import map demo</title> <script type="importmap">{"imports":{"pricing":"./pricing.js"}}</script> <script type="module" src="./main.js"></script></html>pricing.js
main.js
Predicted output (browser fixture not executed in this package):
18The browser maps pricing to a local module URL and loads it over HTTP.
Common mistake
An import map is not a package installer or bundler. Its target must still be a valid reachable module resource.
Practice
Change the bare name to rates in both the map and import statement, then test without the map and observe the resolution error.
Run this practice in the documented local environment. ByteMind module and asynchronous grader support is not yet verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Browser import maps — Knowledge Check
Reason about the concept and predict its behavior in the indicated environment.
Which HTML script type declares an import map?
What does an imports mapping associate?
Does a browser import map install a missing package?
What should be registered before the displayed module request?
For Browser import maps, what is the console output of example 1 in the browser fixture?
// File: index.html<!doctype html><html lang="en"><meta charset="utf-8"><title>Import map demo</title><script type="importmap">{"imports":{"pricing":"./pricing.js"}}</script><script type="module" src="./main.js"></script></html>
// File: pricing.jsexport const unit = 6;
// File: main.jsimport { unit } from "pricing";console.log(unit * 3);