npm Publish Contents · guide

Why adding a .npmignore publishes your gitignored files

You have a .gitignore that keeps .env, coverage/ and your scratch notes out of git. You add a .npmignore with src/ and test/ in it, to keep the package small. The next npm publish ships your .env.

Nothing is broken. npm reads one ignore file per folder: the .npmignore if there is one, otherwise the .gitignore. The moment a .npmignore exists, every line of the .gitignore beside it stops counting for npm.

What npm reads, in order

In every folder of your package, npm (through its npm-packlist module) applies three sets of rules, and the last one that matches a path decides:

  1. Its default excludes: .git, .DS_Store, *.orig, npm-debug.log, .npmrc, editor swap files and the ignore files themselves.
  2. The folder's own rules. At the package root: files in package.json if it is set (and then neither ignore file at the root is read), otherwise .npmignore, otherwise .gitignore. In every sub-folder: its .npmignore, otherwise its .gitignore, and these still apply under files.
  3. Its forced rules: package.json, and at the root any README, LICENSE, LICENCE or COPYING, always ship, as do the files named by main, browser and bin. The root node_modules, lockfiles and .npmrc never do.

The patterns are minimatch globs, not git's: case is ignored, a pattern with no slash matches that name at any depth, and spaces at either end of a line are trimmed.

The same package, two answers

The tool's example is a small library with this .gitignore and .npmignore:

==> .gitignore <==
node_modules/
dist/
coverage/
.env
notes/

==> .npmignore <==
src/
test/
tsconfig.json

With only the .gitignore, npm leaves out .env, coverage/ and notes/. You might expect it to leave out dist/, the built code, too, which is the usual reason people add a .npmignore; it does not, for a reason covered below. With both files, the tool reports that .env, coverage/lcov.info, notes/todo.md and the three dist/ files ship because the .gitignore is not read; for .env it says no rule matches, so it ships. The dist/ files are wanted. The other three are not.

The fix

Prefer an allow-list. With "files": ["dist", "bin"] a new file ships only if it is inside a listed folder, so the next .env or secrets.json stays home. If you keep a .npmignore, copy into it every line of the .gitignore that you still need, starting with .env*.

A second trap: "main": "./dist/index.js"

npm promises to always pack the main file. It turns main into a forced rule, !/ followed by the path, and minimatch keeps a ./ as a path segment of its own, so !/./dist/index.js matches nothing. With "files": ["lib"] and "main": "./dist/index.js", npm 10.9.9 and 11.21.3 both leave dist/index.js out; written as "dist/index.js" it ships. bin paths are cleaned before this step, so "./bin/cli.js" works there. Most packages never notice, because their files already lists the folder.

A third: a folder rule stops working once npm goes in

npm leaves a folder out by never entering it. If anything gives it a reason to enter, a forced main or bin inside the folder, a folder in files, or a ! rule for a file inside, it goes in, and a rule like dist/ or dist names the folder, not the files in it, so they all ship. In the example, main is dist/index.js, so with only the .gitignore npm also ships dist/index.d.ts and dist/index.js.map despite the dist/ line. npm 10.9.9 and 11.21.3 do the same. Here that happens to be what you want. It is not with this .npmignore:

secrets/
!secrets/public.pem

git reads those two lines as "ignore the folder" (it never re-includes a file under an ignored folder). npm reads the ! line as a reason to enter secrets/, and then publishes secrets/private.pem along with the public one, on both npm versions. A rule that names the files, secrets/**, keeps working inside the folder; the tool flags any file that ships from under a folder rule.

Check before you publish

npm pack --dry-run prints the file list but not the reasons. npm Publish Contents takes the same list, or your whole folder, and gives the deciding rule for each file, with the secrets, missing entry points and shadowed .gitignore called out first. In CI, node cli.mjs . --fail-on high stops the job before a secret goes public.

Questions

Does npm use .gitignore when there is a .npmignore?

No. In each folder npm reads the .npmignore if there is one and skips the .gitignore beside it. At the package root, a files field in package.json switches off both. Ignore files in sub-folders are still read, one per folder, with the same preference.

Is files or .npmignore better for keeping secrets out of an npm package?

files, because it is an allow-list: a new file ships only if you list it or it sits in a listed folder. A .npmignore is a deny-list, so every new file ships until you remember to add a line. Either way, check the list with npm pack --dry-run before you publish.

I published a .env file to npm. Is unpublishing enough?

No. Treat every secret in it as leaked: rotate the keys first. Registry mirrors and caches may already hold the tarball, and npm only allows unpublishing within its own policy limits. Then fix the rules and publish a new version.