skip to content

In a web build, why does a browser bundle receive a package's server file when its `exports` map lists `import` before `browser`?

level: middleimportance: must knowfreq 14%

answer

  1. which conditions are active
  2. the package's key order decides
  3. first active key wins
  4. browser and import both match
  5. most specific first, default last

basics

~20 s

A resolver walks the exports entry's keys in the package's own order and takes the first condition it has active. A browser build importing with import has both import and browser active, so the earlier import key wins.

solid answer

~40 s

A resolver brings a set of **active conditions**: how the package is loaded (`import` or `require`), the environment (`browser` for a browser build), often `development` or `production`, any custom ones, and `default`, which always matches. It does not rank them itself. It reads the entry's keys **in the order the package wrote them** and stops at the first one that is active. With `"import"` listed before `"browser"`, a browser build loading the package through `import` matches `import` first and takes the server file. The fix belongs in the package: put the most specific condition first, for example a `browser` object with its own `import` and `require` files, and `default` last. Until the maintainer ships that, point the specifier at the browser file in your own build or patch the installed manifest.

code

json · 12 lines
json
{
  "name": "date-lib",
  "exports": {
    ".": {
      "import": "./dist/node.mjs",
      "browser": "./dist/browser.mjs",
      "require": "./dist/node.cjs",
      "default": "./dist/browser.mjs"
    },
    "./locale/*": "./dist/locale/*.js"
  }
}

go deeper

for a junior

Recall that an exports map chooses a package's file by conditions such as import, require, browser and default, and that it hides paths it does not list.

for a middle

Explain the matching rule exactly: the resolver's set of active conditions, the package's key order, first active key wins, nesting, and why default belongs last.

for a senior

Diagnose a wrong entry from the map and the build's active conditions, choose a consumer-side stopgap that does not rot, and spot when import and require pull two files of one package into one graph.

for a principal

Set the rule for how the organisation's internal packages write their maps (condition order, custom conditions for workspace source, what is exported) so every app's build resolves them the same way.

## What an `exports` map is The `exports` field in a package's `package.json` declares the package's public entry points. Each key is a **subpath** (`"."` for the package name itself, `"./locale/*"` for a pattern), and each value is either a file path or a **conditions object** that chooses a file depending on how and where the package is being loaded. When an `exports` map resolves an import, the legacy entry fields (`main`, `module`, `browser` as a string) are not consulted for it. ## Active conditions and object order Every resolver arrives with a set of **active conditions**. A browser build typically activates: - `import` when the importer used `import` or `import()`, or `require` when it used `require()`; the two are mutually exclusive. - `browser`, a community condition meaning "any web browser environment". - `development` or `production`, depending on the build mode; the two are mutually exclusive. - any **custom conditions** the project enables, such as a workspace's `source` condition. - `default`, the generic fallback that always matches. The rule that surprises people is that the resolver does **not** rank these. The package-manifest rules say key order is significant: the resolver visits the conditions object's keys **in the order they appear in the package** and takes the first key that is active. If that key's value is another conditions object, it recurses into it. The guidance that follows from the rule is that conditions go from most specific to least specific, and `default` always last. ## The failure, step by step The date library's map is shown in the code example. For a browser build that writes `import { parseISO } from 'date-lib'`: 1. Subpath `"."` matches the bare package name. 2. Key `import`: active, because the importer used `import`. The resolver stops here and returns `./dist/node.mjs`. 3. `browser` is never reached, although it is active too. The server file may import built-in server modules or use APIs the browser lacks, so the symptom ranges from a build error about a missing built-in to a run-time failure in the browser. A server bundle resolving the same map gets the same file, which is why the bug often hides until someone inspects the browser output. ## Fixing it | Who | Fix | |---|---| | Package maintainer | Put `browser` first and nest `import` / `require` inside it, then the server entries, then `default` | | Consumer, short term | Point the specifier at the browser file in your build's own configuration, or patch the installed manifest with your package manager | | Consumer, long term | Report it upstream so the next release fixes the order | A corrected map reads: `"browser": { "import": "./dist/browser.mjs", "require": "./dist/browser.cjs" }`, then `"import": "./dist/node.mjs"`, `"require": "./dist/node.cjs"`, and finally `"default": "./dist/browser.mjs"`. Now a browser build hits `browser` first, and a server build, where `browser` is not active, falls through to the server entries. ## Related behaviour worth knowing - **`import` and `require` pick different files.** If the app imports the package with `import` in one module and `require` in another, both files enter the graph. What then goes wrong at run time is the dual-package hazard, a separate subject. - **Unlisted subpaths are blocked.** With an `exports` map, `date-lib/dist/locale/fr.js` fails to resolve even though the file exists, unless a key or pattern such as `"./locale/*"` exposes it; import the exported subpath instead. - **`default` placed first shadows everything.** Because it always matches, nothing after it can ever be chosen. - **`development` and `production`** let a package ship a verbose build to development and a lean one to production, chosen by the build mode. - **Custom conditions** must be enabled in the resolver to take effect; an unknown condition name is simply never active. - **Nested conditions** let one entry honour both the environment and the loading style: the walk enters the `browser` object and applies the same first-active-key rule to the `import` and `require` keys inside it. - **`types`** is conventionally listed first; type checkers read it, and a JavaScript build leaves it inactive and moves past it. ## How to confirm it 1. Read the package's `exports` map and list, in order, the keys the entry offers. 2. Write down which conditions your build activates for that import. 3. The first key on the list that is in your set is the file you got. Check the build's module list for that path.

  • What happens when code imports `date-lib/dist/locale/fr.js` and the package's `exports` map does not list that path?
    Resolution fails with a "not exported" error, even though the file is on disk: an `exports` map makes every unlisted subpath private. The fix is to import the subpath the map does expose, here `date-lib/locale/fr` through the `./locale/*` pattern, or to ask the maintainer to export it. Pointing at the file's absolute path gets around the map, but it ties your code to the package's internal layout.
  • How does the `imports` field, with a key such as `#lib/*`, differ from an `exports` subpath?
    `imports` maps **private** specifiers, which must start with `#`, and they work only for modules inside the package that declares them. Matching follows the same rules, conditions and key order included, and unlike `exports` an `imports` entry may point at another package. Because the mapping lives in `package.json`, every tool that follows the manifest rules reads it, which an ad-hoc build alias cannot promise.
  • Why should `default` be the last key in a conditions object?
    `default` always matches, and the resolver stops at the first active key in object order. A `default` placed first therefore wins for every environment and every loading style, and the more specific keys after it are dead text. Placed last, it serves environments the package did not anticipate, which is its purpose.

  • The resolver picks the most specific condition, wherever it appears in the map.
  • A browser build always prefers the `browser` condition over `import`.
  • The build tool's own condition list sets the priority between keys.
  • Any file inside the package can be imported by path, `exports` or not.
  • `default` is only used when no other key is listed.