Sub topic 8
Subpath exports and hiding internal files
Assume you know TypeScript imports and exports, npm package basics, and the purpose of tsconfig.json. This chapter focuses on how the exports field controls which package paths consumers may import.
Why subpath exports matter
A package usually has internal files that are not part of its supported API. For example, a slug library might contain these files:
src/
index.ts
slugify.ts
locale/
index.ts
internal/
normalize.tsThe package should expose only the entry points that consumers need. If consumers import internal files directly, those files become accidental public API. Later, moving or renaming them can break users.
The exports field creates an allowlist. A consumer can import only the package root or subpaths listed there.
{
"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"
},
"./locale": {
"types": "./dist/locale/index.d.ts",
"default": "./dist/locale/index.js"
}
},
"files": ["dist"]
}The "." key represents the package root:
import { slugify } from "@acme/slugify";The "./locale" key represents a public subpath:
import { supportedLocales } from "@acme/slugify/locale";The types condition points TypeScript to declarations. The default condition points runtime tools to JavaScript.
How encapsulation works
When a package has an exports field, Node.js and compatible tools reject paths that are not listed. This import is valid only if ./internal appears in the map:
import { normalize } from "@acme/slugify/internal/normalize.js";With the map above, it fails because the internal path is not exported. The file may still exist in the installed package, but its path is not a supported package entry point.
The package's source layout and public import paths do not need to match. You can move src/locale/index.ts to another internal location later while keeping the public path @acme/slugify/locale stable.
Mapping several related files
You can export multiple explicit subpaths:
{
"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 legitimately need package metadata. Do not use a broad pattern such as "./*": "./dist/*" unless you intend to make every matching build file public.
Check the boundary
Create a small consumer project and test both supported and unsupported paths. The supported import should compile. The internal import should produce a package subpath error.
Run the package build before testing so every target in the export map exists. Also verify the packed artifact with npm pack --dry-run; an export map controls access, while the files field controls which files are included.
Exercise
Why can a file be present in the installed package but still be unavailable through a consumer import?
Show answer
The exports field controls allowed package paths independently of the files included in the package. A file without a matching export entry is hidden from package subpath imports.