Sub topic 5
Module Specifiers: Relative Paths, Bare Names, and File Extensions
This chapter assumes you know the named, default, and namespace import forms from the previous chapter, and that you can run JavaScript in a browser or with Node.js.
A module specifier is the string after from in an import declaration. It is also the string you pass to import(). The runtime turns each specifier into a full URL or file path through a process called resolution. If resolution fails, the whole module graph fails to load, and none of its code runs.
The Three Kinds of Specifier
| Kind | Example | How it resolves |
|---|---|---|
| Relative | `'./math.js'`, `'../lib/util.js'` | Against the URL of the importing module |
| Absolute | `'/src/math.js'`, `'https://cdn.example.com/x.js'` | As a URL (in Node.js, `file:` URLs work) |
| Bare | `'lodash-es'`, `'node:fs/promises'` | Through a lookup table: an import map, `node_modules`, or a built-in |
A relative specifier must start with ./ or ../. If you write 'math.js' without the dot and slash, it becomes a bare specifier, and the runtime looks for a package named math.js.
// src/app.js
import { area } from './math.js'; // src/math.js
import { format } from '../shared/fmt.js'; // shared/fmt.js
import { readFile } from 'node:fs/promises'; // Node.js built-inThe path in a relative specifier is resolved against the importing file's location. The current working directory plays no part. A file at src/app.js that imports './math.js' always gets src/math.js, wherever you launched the program from.
File Extensions Are Not Optional
Native ES module loaders do not guess. The browser fetches exactly the URL you wrote. Node.js's ESM loader also requires the full file name, and it does not look for index.js inside a directory.
import { area } from './math'; // fails: no extension
import * as shapes from './shapes'; // fails: directories are not resolved
import * as shapes2 from './shapes/index.js'; // worksCommon Resolution Errors
| Error message (typical) | Likely cause |
|---|---|
| `Failed to resolve module specifier "x"` (browser) | Bare specifier with no import map entry |
| `ERR_MODULE_NOT_FOUND` (Node.js) | Missing extension, typo, or package not installed |
| `ERR_UNSUPPORTED_DIR_IMPORT` (Node.js) | Specifier points at a directory |
| `ERR_PACKAGE_PATH_NOT_EXPORTED` (Node.js) | Deep import that the package's `exports` field doesn't allow |
Later chapters cover how browsers map bare names with import maps and how Node.js uses package.json.
Exercise
The file lib/tools/parse.js contains import { clean } from 'helpers/clean.js';, and the target file is lib/helpers/clean.js. Why does this fail, and what specifier fixes it?
Show answer
'helpers/clean.js' doesn't start with ./ or ../, so it is a bare specifier, and the runtime looks for a package named helpers. The specifier is resolved from lib/tools/, so the fix is '../helpers/clean.js'.
Exercise
Will import config from './config'; load config.js when run natively in Node.js as an ES module?
Show answer
No. Node.js's ESM loader does not add extensions, so it throws ERRMODULENOT_FOUND. Use './config.js'.