Skip to content

Lesson 6 of 9 · 26m

`package.json` Exports, `types`, and the Dual-Package Hazard

Publish TypeScript packages whose types and runtime files agree across ESM, CommonJS, bundlers, and older tooling.

Most broken TypeScript packages are not broken because the types are bad. They are broken because the package publishing surface is inconsistent: types points at one file, exports points somewhere else, ESM and CommonJS disagree, and the tarball contains either too much or not enough. Publishing types is release engineering. Treat it that way.

The resolution order has changed under your feet

Older TypeScript tooling relied heavily on top-level fields:

{"main": "./dist/index.cjs", "module": "./dist/index.mjs", "types": "./dist/index.d.ts"}

Modern Node-style resolution gives exports priority. If exports exists and does not expose a path, consumers cannot import it even if main or deep files exist. A modern package usually needs both because consumers are not uniformly modern:

{
  "type": "module", "main": "./dist/index.cjs", "module": "./dist/index.mjs", "types": "./dist/index.d.ts",
  "exports": {".": {"types": "./dist/index.d.ts", "import": "./dist/index.mjs", "require": "./dist/index.cjs"}, "./package.json": "./package.json"}
}

One declaration file cannot always serve two module systems

TypeScript increasingly validates that ESM and CommonJS entrypoints have compatible type interpretations. If both module systems load the same .d.ts, TypeScript may treat it according to the nearest package.json type field and produce confusion. Tools such as @arethetypeswrong/cli exist because this is genuinely hard:

attw --pack .

That command packs the package and checks whether the published type surface resolves correctly across realistic conditions. Run it in CI for libraries. Do not wait for consumer bug reports.

The dual-package hazard is an identity problem

When both ESM and CJS copies are loaded, you can get two instances of the same module:

app imports pkg/index.mjs
plugin requires pkg/index.cjs

Suddenly instanceof fails, singleton state duplicates, and containers contain two versions of everything. The safest mitigation is often one primary module format or stateless entrypoints.

Control the tarball

{"files": ["dist", "README.md", "LICENSE"]}

Then inspect what will actually ship:

npm pack --dry-run

Accidentally shipping src, tests, sourcemaps with private paths, or a broken dist layout is not theoretical. Teams do it every week.

Field notes

  • Deep imports that bypass exports may work in one resolver and fail in Node; decide whether they are public API.
  • typesVersions is still needed for older TypeScript versions but is easy to misuse as a substitute for clean exports.
  • Do not point types at source .ts files unless you intentionally ship source and understand the compile burden.
  • Test installation from the packed tarball in a clean fixture, not just from the workspace.
  • If consumers patch node_modules, your package contract is failing somewhere. Find the missing entrypoint or type condition before adding hacks.

Was this lesson helpful?

Sign in to rate