A real import map in a sandboxed frame: this browser itself resolves bare names, trailing-slash prefixes and scopes to URLs, with its own TypeError when it…
Why it matters
Import maps make ES modules run straight in the browser (CDNs like esm.sh, pages with no build, prototypes) and explain what a bundler really does for you. Three of their rules often surprise: subpaths need an entry ending in a slash, scopes choose by the importing module, and relative specifiers are matched as URLs.
The key idea
The map is read once per import, from the importing module’s URL
Common mistakes
Forgetting the slash in the key for subpaths The key "rupiah" matches only rupiah exactly. For rupiah/format.js add a second entry "rupiah/": ".../[email protected]/", a slash in the key and in the address; a prefix entry whose address has no slash is dropped by the browser.
The import map loads after the first module Put <script type="importmap"> in <head>, before any <script type="module"> and modulepreload. Older browsers and Firefox refuse a map that arrives late (every bare name becomes a TypeError); those with multiple import maps merge it, but rules for names already resolved do not change any more.
Thinking the map never touches relative paths or full URLs A bare key ("struk") never matches ./struk.js, true. But a relative specifier is first read into a full URL, then that URL is looked up in the map: the key "./struk.js" or its full URL remaps that file, even one from a CDN.
Try it in this playground
Meet the bare-name TypeError Leave a bare name with no entry and read this browser’s words.
Use a prefix entry Get rupiah/format.js found through "rupiah/".
One name, two versions Give kalender rupiah 1 through scopes.
Add the map late Move the map after the first module and see what this browser does.