skip to content

In a web build, how does a bundler resolve `'./utils'` and `'date-lib'` to files, and why would a browser's native module loader reject both?

level: juniorimportance: should knowfreq 15%

answer

  1. relative versus bare
  2. join with the importer's folder
  3. extension list, then index file
  4. climb through node_modules folders
  5. browsers resolve URLs, not files

basics

~20 s

A relative specifier is joined to the importer's folder and tried as written, then with each configured extension, then as a folder's index file. A bare one is looked up in node_modules folders, climbing upward. Browsers do neither.

solid answer

~40 s

For `'./utils'` the resolver joins the path to the importing file's folder, tries the path as written, then each extension in its configured list in order (for example `.ts`, then `.js`), and if `utils` is a folder, looks for an entry file inside it, usually `index` plus an extension. For `'date-lib'` it looks for a `date-lib` folder in `node_modules` beside the importer, then in each parent folder's `node_modules`, and reads that package's `package.json` to pick the entry file. A browser loading native modules does neither: it resolves a specifier as a URL relative to the importing module's URL, so `./utils` is fetched as that exact URL with no extension added, and a bare specifier throws a resolution error unless an import map defines it.

code

javascript · 8 lines
javascript
// src/cart/total.js
import { fmt } from './utils';
// tried in order: src/cart/utils, src/cart/utils.ts, src/cart/utils.js, ...
// then src/cart/utils/ as a folder: src/cart/utils/index.ts, index.js, ...

import { parseISO } from 'date-lib';
// tried in order: src/cart/node_modules/date-lib, src/node_modules/date-lib,
// node_modules/date-lib ... then date-lib/package.json picks the entry file

go deeper

for a junior

Recall the two lookups: a relative path joined to the importer's folder with extensions and index files tried, and a bare name searched for in node_modules folders. Know that browsers do neither.

for a middle

Explain the order of attempts, that the extension list is configurable and differs between tools, and how the package's manifest takes over once its folder is found.

for a senior

Use the lookup to diagnose: a stale compiled file shadowing its source, an alias one tool knows and another does not, a bare name resolving to a different copy from a different folder.

for a principal

Argue for a codebase convention, such as full relative paths with extensions or one shared alias definition, that keeps the build, the tests and native module loading agreeing on every file.

## Two kinds of specifier A **module specifier** is the string after `from` (or inside `import()`). Resolvers sort it into one of three kinds before they do anything else: - **Relative**: starts with `./`, `../` or `/`, and names a path near the importing file. - **Absolute**: a full URL or an absolute file path, used as it is. - **Bare**: anything else, such as `date-lib` or `date-lib/locale/fr`. It names a **package**, not a path. The ECMAScript standard does not define resolution; each host does. Browsers follow the HTML standard, and build tools follow conventions that grew out of server-side JavaScript. ## How a bundler resolves `'./utils'` 1. Join the specifier to the **directory of the importing file**: `src/cart/total.js` importing `./utils` gives `src/cart/utils`. 2. If a file exists at exactly that path, use it. 3. Otherwise try each extension in the resolver's **extension list**, in order: `utils.ts`, `utils.js`, `utils.json`, and so on. The first file that exists wins. 4. If `utils` is a directory, look for its entry file, usually `index` combined with the same extension list (`utils/index.js`). A directory with its own `package.json` may name the entry there. 5. If nothing matches, the build fails with an "unable to resolve" error that names the importer. The extension list and its order are configuration, and the defaults **differ between build tools**. If a folder holds both `Button.ts` and `Button.js`, the one whose extension comes first in the list is chosen silently and the other is ignored, which is how a stale compiled file can shadow its source. Some setups put platform-specific extensions such as `.web.js` ahead of `.js` in the list, so a platform variant wins over the shared file whenever both exist. ## How a bundler resolves `'date-lib'` 1. Starting in the importer's directory, look for `node_modules/date-lib`. 2. If it is not there, move to the parent directory and look again, up to the filesystem root (some setups add extra search folders). 3. Once the package folder is found, read its `package.json`. An `exports` map, if present, decides which file the package name (or a subpath such as `date-lib/locale/fr`) maps to, and blocks paths it does not list. Without one, the legacy entry fields and finally an `index` file decide. 4. The chosen file is then resolved like any other path. Because the search climbs from the **importer**, two files in different folders can resolve the same bare name to two different installed copies. That is the root of most "why is this package here twice" puzzles in workspaces. ## What a browser's native loader does instead | Specifier | Bundler | Browser, native modules | |---|---|---| | `./utils` | tries extensions and index files | fetches the URL `./utils` relative to the module's URL, nothing more | | `./utils.js` | uses the file | fetches that URL | | `date-lib` | climbs `node_modules`, reads `package.json` | throws a resolution error unless an import map maps it | The HTML standard forbids one specifier from implicitly resolving to several candidates, because every guess would cost the browser a network request: `./utils`, `./utils.js`, `./utils/index.js` and so on. A browser therefore resolves a specifier as a **URL**, relative to the URL of the module that wrote it. If the server answers `/src/cart/utils` with a 404, or with an HTML fallback page, the import fails. An **import map** (`<script type="importmap">`) lets a page map bare names to URLs, but it is an explicit table, not a search. ## Aliases: a rewrite before the lookup Many builds add an **alias**, such as `@app/` meaning `src/`. An alias is a build-time rewrite of the specifier's prefix, done before the steps above. It is not part of any standard, so every tool that reads the same source (the type checker, the test runner, a linter) must be given the same mapping, or each resolves the import differently. ## Why this matters in practice - Code that relies on extension guessing works only where a resolver guesses; served as native modules, or run by a strict runtime, it fails. - Writing full relative paths with extensions removes a whole class of "works in the build, fails elsewhere" bugs. - A wrong file chosen by extension order produces no error at all, only surprising behaviour. - A directory import such as `'./components'` depends on index-file lookup, which a browser never performs, so it is the first thing to break when the same code is served unbundled. - Reading the resolver's error message (importer, specifier, paths tried) usually shows exactly which step failed.

  • Why can an `@app/` alias make the build pass while the type checker or the test runner reports the import as missing?
    An alias is a rewrite applied by whichever resolver knows about it; it is not part of the module standard or the package manifest. The build's resolver turns `@app/cart` into `src/cart` before looking it up, but a tool that was not given the same mapping treats `@app/cart` as a bare package name and fails to find it. Every tool that reads the source needs the mapping, ideally from one shared definition.
  • A specifier such as `'date-lib/locale/fr'` names a file inside a package. How is that subpath resolved?
    The resolver first finds the `date-lib` package folder by the usual `node_modules` climb, then resolves `locale/fr` inside it. If the package has an `exports` map, the subpath must match one of its keys or patterns, otherwise resolution fails even when the file exists; without an `exports` map, the path is joined to the package folder and resolved with the usual extension and index-file rules.

  • Browsers add `.js` to an extensionless import when loading native modules.
  • Bare specifiers are always looked up only in the project root node_modules.
  • The extension list is fixed by the language, so every tool picks the same file.
  • A bare specifier works in the browser as long as the package is installed.
  • An alias is a standard feature every tool understands without configuration.