Sub topic 8
Specifiers, Template Literals, and Resolution Rules
This chapter assumes you know static import syntax, the basic import() expression, and how to run ES modules in a browser or Node.js. It covers the string you pass to import(), called the specifier: what forms it can take, how the runtime turns it into a real URL or file, and what changes when you build the specifier at runtime.
Three Kinds of Specifier
A specifier is the string that names the module to load. Runtimes recognize three forms:
| Kind | Example | Resolved by |
|---|---|---|
| Relative | `'./chart.js'`, `'../utils/math.js'` | URL of the importing module |
| Absolute URL | `'https://cdn.example.com/lib.js'`, `'/static/app.js'` | Used as is (a leading `/` is relative to the origin) |
| Bare | `'lodash-es'`, `'sharp'` | Node's `node_modules` lookup, or an import map in browsers |
Relative to the module, not the page
A relative specifier is resolved against the URL of the module that contains the import() call. It is not resolved against the HTML page or the current working directory. If src/views/home.js calls import('./chart.js'), the runtime loads src/views/chart.js, wherever the app was started from.
Some cases are different. When import() runs in a classic (non-module) script, relative specifiers resolve against the document's base URL.
Extensions are required
Browsers and Node's ES module loader do not guess file extensions or look for index.js. import('./chart') fails unless a file named exactly chart exists. Bundlers often relax this rule, so code that works in a bundled build can break when the same code runs natively.
Bare specifiers in browsers
Node resolves 'lodash-es' by searching node_modules and reading the package's exports field. Browsers have no package lookup, so a bare specifier throws unless an import map tells the browser where it points:
<script type="importmap">
{ "imports": { "lodash-es": "/vendor/lodash-es/lodash.js" } }
</script>
<script type="module">
const { debounce } = await import('lodash-es');
console.log(typeof debounce); // "function"
</script>Computed Specifiers
Static import requires a string literal. import() accepts any expression, and the runtime converts the result to a string. Template literals and variables both work:
// i18n.mjs
const SUPPORTED = new Set(['en', 'fr', 'de']);
export async function loadLocale(lang) {
if (!SUPPORTED.has(lang)) {
throw new Error(`Unsupported locale: ${lang}`);
}
const mod = await import(`./locales/${lang}.js`);
return mod.default;
}
// See exactly which URL a specifier resolves to, without loading it
console.log(import.meta.resolve('./locales/en.js'));import.meta.resolve() returns the resolved URL as a string. It works in current browsers and in Node 20.6 and later. Use it to debug "module not found" errors.
What tools can see
Runtimes resolve any string, but build tools only know what they can read in the source. A template literal with a fixed prefix and suffix, such as ` ./locales/${lang}.js , gives a bundler a pattern it can match against files. A bare variable, as in import(path)`, gives it nothing to work from. The next chapter covers what bundlers do with each case.
Exercise
The file app/pages/settings.js runs await import('../lib/format') in a browser with no bundler, and the request fails with a 404. The file app/lib/format.js exists. What is wrong, and how do you fix it?
Show answer
The specifier has no file extension. Browsers do not add ".js", so the browser requests app/lib/format, which does not exist. Change the specifier to '../lib/format.js'. It resolves against app/pages/settings.js, so it correctly points to app/lib/format.js.
Exercise
Why is import(./plugins/${name}.js) easier for a bundler to handle than import(name), and what should you add before either call when name comes from a URL query string?
Show answer
The template literal has a fixed directory and extension, so the bundler can match files in ./plugins/*.js. A bare variable could be anything, so the bundler cannot tell which files to include. Before either call, check name against an allowlist of known plugins so that crafted input cannot load other modules.