Sub topic 11
Choosing what gets published with files and npm pack
This chapter assumes you can build a library with tsc, you know what the exports field does, and you have run npm publish at least once. Here you decide which files end up in the tarball (the compressed archive that npm uploads) and check that decision before release.
A package that builds fine on your machine can still fail for consumers. The usual cause is a missing file. Sometimes dist was never uploaded, and sometimes a declaration map points at a source file that was left out. The opposite mistake is shipping too much: tests, .env files, or your whole src folder. Both are cheap to prevent once you know how npm decides what to include.
How npm decides what to include
When you run npm publish or npm pack, npm builds a list of files from the package directory. It applies these rules in order of strength:
- Some files are always included, whatever your settings:
package.json, the README, the LICENSE file, and the file named by themainfield. - Some files are always excluded:
.git,node_modules(unless listed inbundledDependencies),.npmrc, and lockfiles such aspackage-lock.json. - Otherwise, if
package.jsonhas afilesarray, only the entries in that array are included. This is an allowlist. - If there is no
filesarray, npm uses a.npmignorefile as a denylist. If there is no.npmignoreeither, it falls back to.gitignore.
The fallback to .gitignore is the trap for TypeScript libraries. You almost certainly ignore dist in Git, because it is build output. A package with no files field and no .npmignore would then publish without its compiled code.
Prefer the files allowlist over .npmignore. With a denylist, every new file you create is published unless you remember to exclude it. With an allowlist, every new file stays out unless you add it. For a secret file or a scratch folder, the second default is the safer one.
Setting the files field
For the @acme/slugify library, the build writes everything to dist, so the allowlist is one entry:
{
"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"]
}An entry can be a directory or a glob pattern. A directory entry includes everything inside it. You can also exclude paths from an included directory by starting a pattern with !, which is useful for test files that the compiler placed in dist:
{
"files": ["dist", "!dist/**/*.test.*"]
}Fixing the cause is better than filtering. If test files land in dist, your tsconfig.json is compiling them. Add an exclude entry so that tsc never emits them:
{
"include": ["src"],
"exclude": ["src/**/*.test.ts"]
}This keeps the compiled test code, and any declaration files for it, out of your build output.
Every path in exports must be in the tarball
The files field and the exports map are separate settings, and npm does not check that they agree. Every path that exports, main, or types points at must match an included file. If you add the subpath export ./locale that points at ./dist/locale/index.js, files: ["dist"] already covers it. If you later move that output to a folder called lib, the build succeeds and the tests pass. Consumers then get a "module not found" error, because the folder is not in the tarball.
Declaration maps and the src folder
Your tsconfig.json sets declarationMap and sourceMap to true. Each .d.ts.map file records the path of the original .ts file, relative to the map. Editors use that path to jump from a type in dist/index.d.ts to your source when the user chooses "Go to Definition".
With files: ["dist"], the src folder is not in the tarball. The maps then point at files that do not exist, and the editor falls back to the .d.ts file. Nothing breaks, but the maps are dead weight. You have two choices:
- Publish
srcas well:"files": ["dist", "src"]. Go to Definition lands on real source. The tarball gets larger, and the source code is public. For an open-source library, that is usually what you want. - Turn the maps off by setting
declarationMapandsourceMaptofalse. The tarball is smaller, and consumers lose source navigation.
Do not leave the maps on while omitting src. That ships files that cannot work.
Previewing the tarball with npm pack
npm pack creates the tarball locally and runs no upload. Adding --dry-run lists the files without writing the archive. Build first, because the list reflects what is on disk right now:
npm run build
npm pack --dry-runThe output looks like this:
npm notice Tarball Contents
npm notice 1.1kB README.md
npm notice 412B dist/index.d.ts
npm notice 188B dist/index.d.ts.map
npm notice 156B dist/index.js
npm notice 301B dist/index.js.map
npm notice 498B dist/slugify.d.ts
npm notice 214B dist/slugify.d.ts.map
npm notice 489B dist/slugify.js
npm notice 702B dist/slugify.js.map
npm notice 701B package.json
npm notice Tarball Details
npm notice name: @acme/slugify
npm notice version: 1.0.0
npm notice filename: acme-slugify-1.0.0.tgz
npm notice package size: 2.4 kB
npm notice unpacked size: 4.1 kB
npm notice total files: 10Read this list as a checklist:
- Every path named in
exports,main, andtypesappears. - No
.env,.test.js,tsconfig.json, orsrcfiles appear unless you meant them to. - The total file count and package size are close to what you expect. A size of several megabytes for a small utility library means something large slipped in.
For scripts and CI, npm pack --dry-run --json prints the same data as JSON, including a files array. You can search it for forbidden paths.
To get a real tarball for later testing, drop --dry-run:
npm pack
tar -tzf acme-slugify-1.0.0.tgzEvery path in the archive begins with package/, which is how npm lays out tarballs. The file you create is the one you can install into a separate test project with npm install ../slugify/acme-slugify-1.0.0.tgz, which the verification chapter uses.
Stale build output
tsc writes files but never deletes them. Suppose you rename src/locale.ts to src/i18n.ts. The next build creates dist/i18n.js and leaves the old dist/locale.js where it was. Since files includes all of dist, the dead file is published. A consumer who imports it gets code that no longer matches your source.
Clean the output before each build. A script that works on every platform, using Node's own tooling, is:
{
"scripts": {
"clean": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\"",
"build": "npm run clean && tsc",
"prepack": "npm run build"
}
}The prepack script runs before a tarball is created, both for npm pack and for npm publish. That makes it a better guard than prepublishOnly, which runs only on npm publish. With prepack in place, the tarball you inspect locally is built the same way as the one you publish.
Keeping secrets and noise out
The allowlist does most of the work, but check these items:
| File or folder | Why it matters | How to keep it out |
|---|---|---|
| `.env`, key files | Credentials become public the moment you publish | Not in `files`, and always listed in `.gitignore` |
| `src/` | Exposes source; may break declaration maps | Include on purpose or omit the maps |
| `*.test.js` in `dist` | Adds size and confuses editors | `exclude` in `tsconfig.json` |
| `tsconfig.json`, lint configs | Useless to consumers | Not in `files` |
| `*.tgz` from earlier packs | Nests an old tarball inside a new one | Delete it, or ignore it in Git; `.gitignore` is not used when `files` exists, so keep it out of the package folder |
A published version cannot be changed. You can deprecate it or, within a limited window, unpublish it, but a leaked credential must be treated as compromised and rotated. Run npm pack --dry-run before every first release and whenever you change files, exports, or the output folder.
Exercise
Your package.json has no files field and no .npmignore, and your .gitignore contains dist. You run npm run build and then npm publish. What do consumers get, and what one-line change fixes it?
Show answer
npm falls back to .gitignore as a denylist, so dist is left out. The tarball contains package.json, the README, and the other tracked files, but none of the compiled JavaScript or .d.ts files, so every import fails. Adding "files": ["dist"] to package.json fixes it, because files takes priority over .gitignore. Running npm pack --dry-run beforehand would have shown the missing dist entries.
Exercise
You publish files: ["dist"] with declarationMap: true in tsconfig.json. A consumer chooses Go to Definition on slugify and lands in dist/index.d.ts instead of your TypeScript source. Why, and what are two ways to fix it?
Show answer
Each .d.ts.map file points at the original .ts file under src, but src is not in the tarball, so the editor cannot follow the map. Either add src to files so the sources ship with the maps, or set declarationMap (and sourceMap) to false so no broken maps are published.