Sub topic 8
Subpath exports and hiding internal files
This chapter assumes you can write a basic exports map with a types condition and that you know how a package is laid out after tsc has built it. Here you add more entry points, and you make sure consumers cannot reach files you did not intend to publish as API.
Why encapsulation matters
Before exports existed, every file in a package was reachable. A consumer could write import "@acme/slugify/dist/internal/regex.js" and it worked. Once people did that, you could not rename or delete that file without breaking them.
The exports field changes this. When it is present, Node.js and modern bundlers allow only the paths listed in it. Any other path fails with an error such as ERR_PACKAGE_PATH_NOT_EXPORTED. Your public API becomes exactly what you list, and everything else is free to change in a minor or patch release.
Adding a subpath export
A subpath export is an extra key in exports that begins with ./. The key is what the consumer types after the package name. The value says which file to load.
Suppose the library gains a locale helper in src/locale/index.ts. After the build, it lives in dist/locale/index.js. This map exposes it as @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"
}
}
}A consumer now writes:
import { slugify } from "@acme/slugify";
import { transliterate } from "@acme/slugify/locale";The key "./locale" is a public name. It does not have to match the file path. You could map "./text" to ./dist/locale/index.js and keep the folder name private. Choose keys you are willing to keep stable, because renaming a key is a breaking change.
Keep the key and the file in sync
Each subpath needs its own types condition. TypeScript looks up the subpath in exports and then reads the types entry for that key. If you only provide the . entry's types, @acme/slugify/locale resolves to JavaScript with no declarations, and consumers see an error such as "Could not find a declaration file for module".
For this to work, tsc must emit dist/locale/index.d.ts. With rootDir set to src and outDir set to dist, the folder structure of src is copied into dist. So src/locale/index.ts produces dist/locale/index.js and dist/locale/index.d.ts. Check that the paths in exports match that output exactly.
Hiding internal files
Hiding a file takes no extra setting. You hide a file by leaving it out of exports. Suppose the build output looks like this:
dist/
index.js
index.d.ts
locale/
index.js
index.d.ts
internal/
patterns.js
patterns.d.tsOnly ., ./locale and ./package.json are listed. A consumer who writes @acme/slugify/internal/patterns.js gets an error, even though the file is in the published folder. The code in dist/index.js can still import ./internal/patterns.js, because relative imports inside the package are not checked against exports. The map only governs imports that come from outside.
This gives you a simple rule. Public files are those named in exports. Everything else, including every .d.ts file in internal, is private by convention and enforced by tooling.
Patterns: exporting many files at once
Listing every file becomes tedious when you have dozens of modules. A key with one * is a subpath pattern. The * matches any string, and the same string replaces * in the value.
{
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./locale/*": {
"types": "./dist/locale/*.d.ts",
"default": "./dist/locale/*.js"
}
}
}With this map, @acme/slugify/locale/de resolves to dist/locale/de.js and dist/locale/de.d.ts. The * can match text that contains slashes, so @acme/slugify/locale/de/formal maps to dist/locale/de/formal.js.
Patterns trade precision for convenience. Every file that matches becomes public, including a helper you add later by accident. Use a pattern only for a folder where every file is meant to be public. Keep private helpers in a different folder, such as internal, that no pattern covers.
Excluding a path with null
You can block part of a pattern by mapping a more specific key to null:
{
"exports": {
"./locale/*": {
"types": "./dist/locale/*.d.ts",
"default": "./dist/locale/*.js"
},
"./locale/internal/*": null
}
}Node.js picks the most specific matching key, so @acme/slugify/locale/internal/anything is blocked while other locale files stay available. This is a safety net. Moving private files out of the pattern's folder is clearer.
Choosing the extension in keys
Keys can include a file extension, such as "./locale.js", but extensionless keys like "./locale" read better and let you change the file type later. A pattern key like "./locale/*" follows the same idea. Pick one style across the package.
Common mistakes
| Mistake | Symptom | Fix |
|---|---|---|
| A subpath has `default` but no `types` | Consumer gets an implicit `any` error or a missing declaration error | Add a `types` condition for that key |
| `types` listed after `default` | TypeScript never reaches `types`, because the first matching condition wins | Put `types` first in each object |
| Key does not start with `./` | Package fails to load with an invalid configuration error | Start every subpath key with `./` |
| Path in value does not start with `./` | Same invalid configuration error | Start every target with `./` |
| Output folder differs from the path in `exports` | File not found at runtime | Compare `exports` against the `dist` folder after building |
| Pattern covers private files | Private files become public API | Move private files out, or map them to `null` |
| Old tools ignore `exports` | Deep imports like `@acme/slugify/locale` fail under `moduleResolution: node10` | See below |
Older resolution modes and subpaths
With moduleResolution set to node10, TypeScript does not read exports. It looks for @acme/slugify/locale as a real path, such as node_modules/@acme/slugify/locale.d.ts or node_modules/@acme/slugify/locale/index.d.ts. Neither exists in your package, so those consumers cannot find the types.
If you must support them, add a small stub folder. Create locale/package.json at the package root:
{
"types": "../dist/locale/index.d.ts",
"main": "../dist/locale/index.js"
}Then add "locale" to the files array so it is published. This adds files and a second place to maintain paths. Many libraries decide that node10 consumers get only the root entry, and they say so in the README. Decide how much legacy support your users need before adding stubs.
Verifying your subpaths
After a build, list what would be published and test each entry point from a consumer's point of view.
npm run build
npm pack --dry-run
npx @arethetypeswrong/cli --pack .Are the Types Wrong reports each subpath separately, so a missing types condition on ./locale shows up even when . is correct. In a separate test project that installs the packed tarball, also confirm that a private path fails:
import { slugify } from "@acme/slugify";
import { transliterate } from "@acme/slugify/locale";
// @ts-expect-error - internal files are not exported
import { patterns } from "@acme/slugify/internal/patterns.js";
console.log(slugify("Hello"), transliterate("Straße"), patterns);The @ts-expect-error line is satisfied only if TypeScript reports an error for the third import. If the error disappears, the file has become public and you should check your exports map.
Exercise
Your package exports "." and "./locale". A user reports that import { transliterate } from "@acme/slugify/locale" runs fine in Node.js, but TypeScript says it cannot find a declaration file. The exports entry for "./locale" is { "default": "./dist/locale/index.js" }. What is wrong, and what should the entry be?
Show answer
The entry has no types condition, so TypeScript has no declaration file to use for that subpath. Add one, listed first: { "types": "./dist/locale/index.d.ts", "default": "./dist/locale/index.js" }. Also confirm that tsc emits dist/locale/index.d.ts.
Exercise
Your dist/internal/ folder holds helper modules. Your exports map contains the pattern "./" mapped to "./dist/.js". Why is this a problem, and how would you fix it?
Show answer
The pattern makes every file in dist, including dist/internal/, importable by consumers, so the helpers become public API. Replace the broad pattern with explicit keys, or with a pattern that covers only a folder of public modules, such as "./locale/". You could also add "./internal/": null, but moving the private files out of the matched folder is clearer.