Skip to content

Lesson 2 of 9 · 21m

Package Exports, Resolution, and Lockfiles

How Node finds a dependency, what exports maps really control, and why install tooling is production infrastructure rather than ceremony.

The moment an application grows beyond one service, dependency resolution stops being package-manager trivia. It becomes a release-control system. The question is never just “does this install?” It is “which exact files enter production, from which registry, at which version, with which postinstall behavior?”

Resolution is a chain of precise decisions

When Node evaluates import "acme-http/retry", it walks upward looking for node_modules/acme-http/package.json, then consults exports:

{
  "name": "acme-http",
  "exports": {
    ".": "./dist/index.js",
    "./retry": {
      "types": "./dist/retry.d.ts",
      "import": "./dist/esm/retry.js",
      "require": "./dist/cjs/retry.cjs"
    },
    "./package.json": "./package.json"
  }
}

The keys are not glob conveniences. If ./retry is absent, deep import is blocked even though the file exists physically. That encapsulation is intentional: it lets maintainers stop supporting accidental internals.

The order of conditions matters. Put "types" first, then runtime conditions such as "import" and "require", and a generic "default" last. TypeScript, bundlers, and Node do not all read the same condition set unless you arrange it explicitly.

Semver is a promise made by strangers

^2.4.1 means “compatible according to the maintainer’s interpretation,” not “safe.” Most production breakage I have seen from dependencies came from perfectly legal minor releases: a transitive dependency dropped an old Node version, changed timing behavior, or tightened input validation.

Your defense is reproducibility:

npm ci
pnpm install --frozen-lockfile
bun install --frozen-lockfile

Use the install mode that trusts the lockfile, not the mode that helpfully updates it. CI should fail if the lockfile and manifest disagree. A lockfile diff is a release diff; review it like one.

Package manager differences are operational, not aesthetic

npm’s flat node_modules, pnpm’s content-addressed store and strict links, Yarn’s PnP, and Bun’s fast installer solve different failure modes. Strict layouts expose phantom dependencies — code that imports a package never declared because hoisting made it visible. That is a feature, even when the first migration is miserable.

{
  "dependencies": {
    "fastify": "^5.2.0"
  },
  "devDependencies": {
    "typescript": "^5.7.2"
  },
  "engines": {
    "node": ">=22.11.0"
  },
  "packageManager": "pnpm@9.15.0"
}

packageManager plus Corepack reduces “works on my laptop” drift. engines is advisory in many setups, so enforce it in CI if it is a real boundary.

Scripts are a trust boundary

preinstall, install, and postinstall execute arbitrary code with your developer or CI credentials. In high-risk environments, disable scripts by default and allowlist the few native packages that need them:

npm config set ignore-scripts true
pnpm config set ignore-scripts true

Then build native dependencies deliberately. Convenience gained by invisible build steps is usually paid back during a supply-chain incident.

Field notes

  • Never import through a transitive dependency. If your code names it, declare it.
  • Check effective resolution with npm ls package, pnpm why package, or bun pm why package when duplicates appear.
  • Registry mirrors and private registries belong in checked-in .npmrc only when they contain no credentials.
  • Commit lockfiles for applications. Libraries should test against a range, but applications need one reproducible dependency graph.
  • Treat lockfile-only PRs as dependency releases, not maintenance noise.

Was this lesson helpful?

Sign in to rate