Sub topic 11
Choosing What Gets Published with `files` and `npm pack`
This chapter assumes you know basic npm commands, package.json, and TypeScript builds. The goal is to publish the files consumers need while keeping source code, tests, and local configuration out of the package.
Use `files` as an allowlist
The files field in package.json lists files and directories that npm may include in the published package. It is usually safer than trying to exclude many files with .npmignore.
For a library that compiles src into dist, use an allowlist:
{
"name": "@acme/slugify",
"version": "1.0.0",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist"
]
}The published package also needs metadata. npm always includes important files such as package.json, a README file, and a license file when they exist. The files field does not mean that only the listed path exists in the tarball. It means that the listed project files are eligible for inclusion in addition to npm's required files.
A typical project can look like this:
slugify/
├── src/
├── test/
├── dist/
├── package.json
├── README.md
├── LICENSE
├── tsconfig.json
└── package-lock.jsonWith "files": ["dist"], consumers receive the compiled JavaScript and declaration files, but normally do not receive src, test, or tsconfig.json.
| Project file | Usually publish it? | Reason |
|---|---|---|
| `dist/` | Yes | Runtime JavaScript and declaration files |
| `src/` | Usually no | Source is not required by consumers |
| `test/` | No | Tests increase package size without being part of the API |
| `README.md` | Yes | npm displays it as package documentation |
| `LICENSE` | Yes | Communicates usage rights |
Inspect the real package
Do not infer package contents from the project directory. npm applies its own inclusion and exclusion rules. Run npm pack --dry-run after building:
npm run build
npm pack --dry-runThe command prints the files that would enter the tarball, along with the package size. Confirm that the output contains:
- The entry JavaScript file named by
mainorexports. - Every
.d.tsfile needed by the public API. - Declaration maps if you intend to ship them.
- README and license files.
- No private configuration, test fixtures, credentials, or large development assets.
npm pack --dry-run does not create the final archive. Running npm pack creates a local .tgz file, which is useful for testing installation in another project.
Keep the allowlist maintainable
A directory entry such as "dist" includes its contents recursively. If you add a public subpath, build its files under that directory so the allowlist continues to cover them.
Avoid publishing files by accident through broad patterns such as "**/*". Also check generated files before each release. A forgotten source map can expose source paths, and a copied environment file can expose secrets.
The exports field controls which published files consumers may import. The files field controls which project files enter the tarball. Use both: files limits package contents, while exports limits the supported import surface.
Exercise
Which command shows the files that would be included in a package without creating an archive?
Show answer
npm pack --dry-run