Sub topic 7
Defining entry points with the exports field
This chapter assumes you can write TypeScript, run tsc, and read a package.json. It also assumes you know the difference between ESM (import) and CommonJS (require) syntax. You will learn how the exports field tells Node.js, bundlers and TypeScript which files a consumer may load, and how to order conditions so that types resolve correctly.
Why exports exists
Before exports, a package had one entry point (main) and every file in the package was reachable by path. A consumer could write import x from "pkg/dist/internal/helper.js" and it worked. Your internal layout then became part of your public API, and renaming a file broke someone.
The exports field replaces that model with an allow-list:
- It names the entry points a consumer may import.
- It can choose a different file depending on how the consumer loads the package (conditions).
- It blocks every path that is not listed. Importing an unlisted path fails with
ERR_PACKAGE_PATH_NOT_EXPORTED.
When exports is present, Node.js ignores main for resolution. main and types stay only as fallbacks for older tools, covered in the previous chapter.
The simplest exports map
For an ESM-only package with one entry point, this is the map from our @acme/slugify library:
{
"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"
},
"./package.json": "./package.json"
},
"files": ["dist"],
"scripts": {
"build": "tsc",
"prepublishOnly": "npm run build"
},
"publishConfig": {
"access": "public"
}
}Each key in exports is a subpath. The key "." means the package root, which is what import "@acme/slugify" loads. Every target path must start with ./ and stay inside the package.
The entry "./package.json" is deliberate. Some tools read a dependency's package.json to check its version. With exports in place, that read is blocked unless you export the file.
Conditions
An object value under a subpath is a set of conditions. A condition is a named key such as types, import, require or default. The resolver walks the keys in order and takes the first one that matches its environment.
Conditions you will meet most often:
| Condition | Matches when |
|---|---|
| `types` | TypeScript is looking for type information |
| `import` | The consumer loads the package with `import` or `import()` |
| `require` | The consumer loads the package with `require()` |
| `node` | The code runs in Node.js |
| `default` | Always matches. Use it as the last fallback |
Two rules matter in practice:
- Order is significant. The first matching key wins, so a more specific condition must come before a general one.
defaultgoes last. Keys placed afterdefaultare never reached.
Why types comes first
TypeScript matches the types condition when it resolves under node16, nodenext or bundler. Node.js and bundlers do not match it, so they skip it and continue. If you put default before types, TypeScript matches default first, gets a .js file, and either finds no types or finds the wrong ones.
{
"exports": {
".": {
"default": "./dist/index.js",
"types": "./dist/index.d.ts"
}
}
}The map above is broken for TypeScript users. The types key is unreachable because default always matches first. Always write:
{
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
}How a resolver walks the map
The diagram shows one decision per level. First the resolver checks whether exports exists, then whether the subpath is listed, and last which condition matches first.
Conditions inside conditions
Conditions can nest. This matters when ESM and CommonJS consumers need different files. Each format gets its own types key, because the declaration file for ESM and for CommonJS are separate files with different module semantics.
{
"name": "@acme/slugify",
"version": "1.0.0",
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
}
},
"files": ["dist"]
}Here types sits inside each of import and require, not beside them. The reason is how TypeScript decides a file's module format. A .d.ts file in a package with "type": "module" is treated as ESM, and a .d.cts file is treated as CommonJS. Pointing a require consumer at index.d.ts makes TypeScript treat the types as ESM, which gives wrong results for default exports. A later chapter covers building this dual layout in full.
Adding subpaths
A package can expose more than one entry point. Each extra entry is another key in exports:
{
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./locale": {
"types": "./dist/locale/index.d.ts",
"default": "./dist/locale/index.js"
}
}
}Consumers can now write import { ... } from "@acme/slugify/locale". Anything under dist that is not listed stays unreachable from outside. The next chapter covers subpath patterns and encapsulation in detail.
What the source and build look like
The exports map points at build output, so the build must produce those exact files. This is the source from the library layout chapter:
// src/slugify.ts
export interface SlugOptions {
separator?: string;
}
export function slugify(input: string, options: SlugOptions = {}): string {
const separator = options.separator ?? "-";
return input
.trim()
.toLowerCase()
.replace(/[^a-z0-9]+/g, separator)
.replace(new RegExp(`^${separator}+|${separator}+$`, "g"), "");
}
// src/index.ts
export { slugify } from "./slugify.js";
export type { SlugOptions } from "./slugify.js";With rootDir: "src" and outDir: "dist", src/index.ts compiles to dist/index.js and dist/index.d.ts. These match the paths in the map. If you change outDir or move index.ts, update exports too, or the package will install but fail at import time.
Common errors
ERR_PACKAGE_PATH_NOT_EXPORTED. A consumer imported a path you did not list. Add a subpath entry if the path should be public. Otherwise the consumer must stop using it.ERR_MODULE_NOT_FOUNDat the target. The map names a file that the build did not produce, or thatfilesexcluded from the tarball. Compare the map tonpm pack --dry-runoutput.- Missing types in the consumer. Either
typescomes afterdefault, or the consumer usesmoduleResolution: "node10", which ignoresexportsentirely. That is whymainandtypesremain as fallbacks. - Target paths without
./. A value like"dist/index.js"is invalid. Every target must begin with./. - Mixing subpath keys and condition keys. At one level of the object, all keys must be subpaths (starting with
.) or all must be conditions. Mixing them makes the package invalid, and Node.js throws when it loads the file.
Quick reference
- Put a
"."key first for the main entry point. - Inside each subpath, order keys from most specific to least:
types, then format conditions, thendefault. - Export
./package.jsonso tooling can read the version. - Keep
mainandtypesas fallbacks for tools that ignoreexports. - Check every target path against
npm pack --dry-run.
Exercise
A consumer on TypeScript with moduleResolution "nodenext" reports that "@acme/slugify" has no type declarations. Your exports entry is { ".": { "default": "./dist/index.js", "types": "./dist/index.d.ts" } }. What is wrong, and what is the corrected entry?
Show answer
Conditions are checked in order and "default" always matches, so TypeScript takes ./dist/index.js and never reaches "types". Put "types" before "default": { ".": { "types": "./dist/index.d.ts", "default": "./dist/index.js" } }.
Exercise
Your package has "type": "module" and ships both dist/index.js and dist/index.cjs. Why should the require condition point to its own types file (index.d.cts) rather than sharing index.d.ts with the import condition?
Show answer
Under "type": "module", TypeScript reads index.d.ts as an ES module. A CommonJS consumer that gets those types sees ESM semantics, which can produce wrong default-export typing. A .d.cts file is read as CommonJS, so the types match the runtime file. Each condition therefore needs its own nested "types" key.