Sub topic 7
Defining Entry Points with the `exports` Field
This chapter assumes that you know TypeScript syntax, npm, package.json, and basic ESM import and export syntax. You should also know how tsc emits files into a dist directory.
The exports field in package.json defines the files that consumers may import from your package. It is the modern way to describe public entry points. It can also select different files for JavaScript and TypeScript consumers.
The package root entry point
Suppose your build produces these files:
dist/index.js, the runtime JavaScriptdist/index.d.ts, the TypeScript declarations
A package can expose them through its root import, such as @acme/slugify, with this configuration:
{
"name": "@acme/slugify",
"version": "1.0.0",
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"files": ["dist"]
}The "." key means the package root. The types condition points TypeScript to the declaration file. The default condition points runtime tools to the JavaScript file.
The declaration file contains the public type information without implementation code. Consumers therefore receive editor completion and type checking while running the compiled JavaScript.
Why condition order matters
Conditions are checked in order. Put "types" before "default" so TypeScript-compatible tools can select the declaration file. A runtime tool does not use the types condition and continues to default.
The main and types fields are older entry points. Keeping them can help older tools, but modern tools generally use exports when it exists. An incorrect exports map can therefore break consumers even when main points to a valid file.
Adding subpath exports
A subpath export gives a name to another public entry point. For example, consumers can import locale helpers with @acme/slugify/locale:
{
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./locale": {
"types": "./dist/locale/index.d.ts",
"default": "./dist/locale/index.js"
},
"./package.json": "./package.json"
}
}The ./package.json entry is optional. Add it only when consumers need package metadata. A source file such as dist/internal.js remains unreachable unless you explicitly export it.
This restriction is useful because it prevents consumers from depending on implementation details. You can reorganize internal files without promising to preserve their paths.
Visualizing resolution
Common errors
- Pointing
typesto a source file that is not published. - Exporting
dist/index.jsbut forgettingdist/index.d.ts. - Using a subpath in documentation without adding that subpath to
exports. - Reaching into an unexported internal file.
- Writing an
exportsmap whose paths do not match the build output. - Assuming
maincan rescue an invalid or incompleteexportsmap.
Check the packed tarball, not only your working directory. The package must contain every file named by the map.
Exercise
What must you add to the exports map if consumers should be able to import @acme/slugify/locale?
Show answer
Add a ./locale entry that maps the types condition to its declaration file and the default condition to its runtime JavaScript file.