Sub topic 7
Reading `package.json` exports and conditions
Assume you can write TypeScript import and export statements, edit tsconfig.json, and run tsc. You should also know that Node.js supports both CommonJS and ES modules.
A package's exports field is a map from an import path to one or more possible files. It controls which parts of the package consumers may import and lets the package choose different files for different environments.
Why `exports` matters
Without exports, tools often inspect fields such as main, module, or types. These fields provide limited control. An exports map can define:
- The public entry points of a package.
- Separate files for
importandrequire. - Separate declaration files for TypeScript.
- Environment-specific files, such as browser and Node.js versions.
- A restriction against importing internal files directly.
For example, this package exposes only its root entry point:
{
"name": "example-lib",
"exports": {
".": "./dist/index.js"
}
}The specifier example-lib matches ".". An import such as example-lib/internal is not allowed unless the map also contains an "./internal" key.
Conditions select the branch
An export can contain conditional branches. A condition is a key that describes the request or runtime. Common conditions include:
| Condition | Used when |
|---|---|
| `import` | The consumer uses an ES module import |
| `require` | The consumer uses CommonJS `require` |
| `types` | TypeScript is looking for declarations |
| `node` | The target environment is Node.js |
| `default` | A fallback branch matches |
The order of conditions matters. Resolution checks the available keys in order and selects the first applicable branch. Put types before runtime fallbacks in a branch so TypeScript can find declarations before it selects a JavaScript file.
{
"name": "example-lib",
"type": "module",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
}
}
}Here, an ES module consumer receives dist/index.js. TypeScript reads dist/index.d.ts for its types. A CommonJS consumer receives dist/index.cjs, while TypeScript uses dist/index.d.cts.
The .d.ts file describes an ES module in this package because the package has "type": "module". The .d.cts file explicitly describes CommonJS. Declaration extensions should match the module format they describe.
How TypeScript reads the map
With module and moduleResolution set to nodenext, TypeScript determines the format of the importing file first. It then evaluates the package's exports map using conditions that match that format.
For a source file treated as ESM:
import { greet } from "example-lib";
console.log(greet("world"));TypeScript follows the import branch and then the types entry. The runtime follows the same general branch and loads the JavaScript default entry.
For a CommonJS source file, TypeScript follows the require branch. The package therefore needs a CommonJS-compatible implementation and matching declarations.
Nested conditions and troubleshooting
Conditions can be nested, so read them from the outside inward:
flowchart TD
A["Import example-lib"] --> B{"ESM or CommonJS?"}
B -->|ESM| C["import branch"]
B -->|CommonJS| D["require branch"]
C --> E["types for checking"]
C --> F["default for runtime"]
D --> G["types for checking"]
D --> H["default for runtime"]caption: How nested export conditions select declarations and runtime files alt: A flowchart showing an import choosing either the ESM or CommonJS branch, then selecting types and runtime files.
If TypeScript reports that a package has no declarations, check these details:
- The package has an
exportsfield that exposes the requested path. - The selected branch contains a
typesentry. - The declaration file exists at the exact path.
- The declaration extension matches the module format.
- The package's
typefield and file extensions agree. - Your
moduleResolutionsetting supports the package's export map.
Exercise
With the exports map above, which declaration file does TypeScript choose for import "example-lib" from an ES module file, and which file does it choose from a CommonJS file?
Show answer
It chooses ./dist/index.d.ts for the ES module import and ./dist/index.d.cts for the CommonJS request.