Sub topic 7
Reading package.json exports and conditions
This chapter assumes you can write TypeScript imports and exports, you know what tsconfig.json and tsc do, and you know that Node.js runs both CommonJS and ES modules. You should also know the main and dependencies fields of package.json. It covers the exports field and how TypeScript uses it under nodenext and bundler resolution.
Why exports exists
Before exports, a package was described mainly by main. A consumer could also import any file inside the package by path, such as import x from "lib/dist/internal/helper.js". That made every file part of the public API.
The exports field replaces that open door with an explicit map. It does two jobs:
- It lists which entry points the package offers.
- It says which file serves each entry point, depending on how the importer loads the package.
When a package has an exports field, Node.js and TypeScript (in node16, nodenext, and bundler resolution) use it and ignore main for package-name imports. A path that is not listed cannot be imported.
The shape of an exports map
Each key in the map is a subpath. The root entry is ".". Each value is either a file path or an object of conditions.
{
"name": "example-lib",
"type": "module",
"exports": {
".": "./dist/index.js",
"./utils": "./dist/utils.js",
"./package.json": "./package.json"
}
}With this map:
import "example-lib"resolves to./dist/index.js.import "example-lib/utils"resolves to./dist/utils.js.import "example-lib/dist/index.js"fails, because that subpath is not a key.
Paths must start with ./. The "./package.json" entry is a common addition because tools often read that file, and it would otherwise be blocked.
Subpath patterns
A single * in a key and its value maps a whole family of files:
{
"exports": {
"./features/*": "./dist/features/*.js"
}
}The text matched by * is substituted into the value. example-lib/features/auth becomes ./dist/features/auth.js. The * can match text that contains /, so example-lib/features/a/b maps to ./dist/features/a/b.js.
Conditions
An object whose keys are not subpaths is a set of conditions. Each key names a situation, and the first key that matches wins. Order therefore matters. Node.js reads the keys from top to bottom and stops at the first match.
The conditions you meet most often are:
| Condition | Matches when |
|---|---|
| `import` | The importer is an ES module `import` statement or dynamic `import()` |
| `require` | The importer is a CommonJS `require()` call |
| `types` | TypeScript is looking for a declaration file |
| `node` | The code runs in Node.js |
| `default` | Always matches; it is the fallback |
Some packages also use browser, development, production, or module. Whether those match depends on the tool. Node.js does not set development unless you pass it with --conditions.
How TypeScript chooses the active conditions
TypeScript decides which conditions are active from your configuration and from the kind of import it is resolving:
- Under
nodenext, animportstatement in an ESM file activatesimport. Arequirecall in a CommonJS file activatesrequire.nodeis always active, and so istypes. - Under
bundler,importis active forimportstatements, andrequireis active forrequirecalls, whichever format the importing file has.typesis active in both modes. - You can add conditions with the
customConditionsoption intsconfig.json.
This is the key difference from the module setting. The condition follows the importing syntax and the importing file's format, so a single file can resolve one package to two different files depending on whether it uses import or require.
A dual-format package
The following map is the standard shape for a package that ships both ESM and CommonJS builds, each with its own declaration 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"
}
}
}
}Read it as a decision tree:
In the diagram, "looking for types" is TypeScript's own pass. TypeScript needs a declaration file, so it matches the types condition. Node.js at runtime does not match types, so it falls through to default.
Why `types` comes first
Because the first matching key wins, types must come before default. If default comes first, it matches immediately for TypeScript too, and TypeScript receives ./dist/index.js. It then looks for a declaration next to that file, and may find none or the wrong one.
{
"exports": {
".": {
"default": "./dist/index.js",
"types": "./dist/index.d.ts"
}
}
}This map is wrong. The types key is unreachable, because default always matches first.
Why each branch has its own types
The type field sets how .js files are read. With "type": "module", index.d.ts describes an ES module and index.d.cts describes a CommonJS module. If the require branch pointed at index.d.ts, TypeScript would treat the types as ESM while the runtime file is CommonJS. This mismatch is often called a masquerading problem. It produces types that claim named exports exist when a require caller actually gets a different shape. Giving each branch its own declaration file avoids it.
Predicting what TypeScript selects
Work through this lookup with the dual-format map above. The consumer is an ESM file:
// consumer/src/main.ts, in a package with "type": "module"
import { createClient } from "example-lib";
console.log(createClient);- The specifier
example-libis a package name, so TypeScript findsnode_modules/example-lib/package.json. - The package has
exports, so TypeScript uses the"."key. - The value is a conditions object. The syntax is
import, so theimportbranch matches. - Inside that branch TypeScript is looking for types, so
typesmatches and the result is./dist/index.d.ts.
Now a CommonJS consumer:
// consumer/src/legacy.cts
import lib = require("example-lib");
console.log(lib.createClient);The require branch matches, so TypeScript reads ./dist/index.d.cts.
Subpaths, extensions, and types
Each subpath you export needs its own types. The simplest layout is to put the types condition inside each subpath:
{
"name": "example-lib",
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./utils": {
"types": "./dist/utils.d.ts",
"default": "./dist/utils.js"
}
}
}This ESM-only package is shorter than the dual-format one. If you only ship ES modules, you do not need require at all. Node.js 22 and later can load ES modules from require() in many cases, but TypeScript still types it according to the declared conditions, so check what your exports map promises.
Common errors
The subpath is not exported
error TS2307: Cannot find module 'example-lib/internal/helper' or its corresponding type declarations.The key ./internal/helper is not in the map. Either use a path the package exports, or ask the author to add it. Importing from dist directly does not work when exports exists.
Types resolve but the runtime fails
TypeScript can find index.d.ts while Node.js cannot load the file named in default. TypeScript does not check that the files in exports exist at runtime. Test the published package by installing it, for example with npm pack and a clean project.
Fallback to main
If a package has no exports field, nodenext and bundler fall back to types, typings, and main. The consumer then gets whatever those fields name, and subpath imports use plain file paths.
Wrong condition order
If an import resolves to the wrong declaration file, check the order of keys. Put types first, then specific conditions such as import and require, then default last.
Inspecting the result
To see which key TypeScript picked, run the compiler with tracing and filter by the package name:
npx tsc --noEmit --traceResolution | grep "example-lib"Look for lines such as Resolving in ESM mode with conditions 'import', 'types', 'node' and the exports entry that matched. A later chapter covers tracing in detail.
Exercise
A package has this exports map. A CommonJS file under nodenext runs const lib = require("pkg"), and TypeScript is checking that call. Which file does TypeScript read for types, and what is wrong with the map?
{ "exports": { ".": { "import": "./dist/index.js", "default": "./dist/index.cjs", "types": "./dist/index.d.ts" } } }
Show answer
The require call does not match import, so TypeScript moves to default, which matches immediately and returns ./dist/index.cjs. The types key after it is never reached. TypeScript then looks for a declaration next to index.cjs, which is index.d.cts, and finds nothing if that file is not shipped. The fix is to put types before default inside each branch, with ./dist/index.d.cts in a require branch, so the map has separate import and require branches that each list types first and default second.
Exercise
Given "./features/": "./dist/features/.js", which file does the import "example-lib/features/auth/login" map to, and does "example-lib/features" match?
Show answer
The first import maps to ./dist/features/auth/login.js, because the * matches "auth/login". The bare "example-lib/features" does not match, because the key requires "./features/" followed by at least some text for the star.