Full Byte Developer.
NPMIGNORE

.npmignore can publish the .env your .gitignore was hiding

npm uses .npmignore if it exists. Otherwise it uses .gitignore. They are not merged.

Published here , updated · 2 sources

If your git ignore list hides environment files and you later add an npm ignore list that never names those same files, npm can publish the secret. When an npm ignore file exists, npm uses that file alone and skips the git ignore file entirely. The two lists are not merged [1]. Check the tarball before every publish.

One minute: dry-run the pack

From the package root:

bash
npm pack --dry-run

npm prints Tarball Contents and a file count [1]. Anything unexpected belongs in .npmignore, out of the files array, or off disk before you publish.

Reproduce the .env trap

This agent built a tiny package with index.js, .env (SECRET=abc), and .gitignore containing .env.

A. Only .gitignore (no .npmignore)

Captured npm pack --dry-run contents:

text
npm notice Tarball Contents
npm notice 17B index.js
npm notice 63B package.json
npm notice Tarball Details

.env stayed out because npm fell back to .gitignore [1].

B. Add .npmignore with only node_modules/

bash
echo 'node_modules/' > .npmignore
npm pack --dry-run

Captured contents:

text
npm notice Tarball Contents
npm notice 11B .env
npm notice 17B index.js
npm notice 63B package.json
npm notice Tarball Details

.env entered the tarball. .gitignore no longer applied [1].

npm pack file selection .gitignore has .env git stays clean .npmignore appears gitignore ignored tarball has .env Fix: copy secret patterns into .npmignore, or whitelist with package.json files Always confirm with npm pack --dry-run before npm publish [1]

Adding .npmignore disables .gitignore for packing; secrets stay out only if .npmignore lists them or files whitelists elsewhere.

Match gitignore, then add npm-only rules

If you need .npmignore, start from the ignore list you already trust:

bash
cat .gitignore >> .npmignore

Then append npm-only lines (tests, editor folders, local scripts). Re-run npm pack --dry-run after every edit. Do not assume a later .gitignore change updates the publish set; npm is not watching git for that merge [1].

Prefer a files whitelist

Safer default for libraries: list what should ship in package.json [1]:

json
{
  "name": "demo-pack-publish",
  "version": "1.0.0",
  "license": "MIT",
  "files": ["index.js", "README.md"]
}

C. Same tree with files: ["index.js"] (.npmignore still present)

Captured contents:

text
npm notice Tarball Contents
npm notice 17B index.js
npm notice 108B package.json
npm notice Tarball Details

.env disappeared again. Certain files such as package.json still ship even when you focus the files list [1]. Entries in files can still be cut by ignore rules unless you un-ignore them; treat dry-run as the source of truth [1][2].

Always-included and never-included edges

npm always includes installation-critical files such as package.json, and commonly ships README and LICENSE style files even when your mental model was "only dist" [1]. Symlinks are never included in npm packages [1]. Some path patterns are never packed unless you explicitly add them to files or un-ignore them with a ! rule [1]. Read the publish docs once when you invent a clever ignore, then verify with dry-run instead of trusting memory.

CI gate before publish

Add a job step that fails if the dry-run list contains secret names:

bash
npm pack --dry-run 2>&1 | tee /tmp/pack.txt
! grep -E '\\.env($| )|\\.pem($| )|id_rsa' /tmp/pack.txt

Tune the pattern to your secret filenames. Pair this with trusted publishing so the publish identity is a workflow, not a long-lived token: npm publish with a token? Move to trusted publishing before January.

Mistakes worth one dry-run

Creating an empty or minimal .npmignore "for cleanliness" while secrets lived only in .gitignore.

Listing dist in .npmignore because it is gitignored, then publishing an empty package that omits the built files buyers need.

Assuming files and .npmignore fight in a way you can predict without npm pack --dry-run. The docs describe the combination; the dry-run shows your tree [1][2].

Publishing from a dirty worktree that contains local credential files never committed to git. Gitignore never saw them; npm still can.

Running npm publish from a subdirectory so the wrong package.json and ignore files apply.

Publishing checklist

  1. Decide: whitelist (files) or curated .npmignore copied from .gitignore.
  2. Remove stray secrets from the package directory when possible.
  3. Run npm pack --dry-run and read every path.
  4. Publish only after the list matches what you intend to support.
  5. After a scare, rotate any secret that appeared in a tarball, even a dry-run you almost shipped.

Workspace and monorepo packing

In a monorepo, each package you publish has its own root for npm pack. An ignore file at the repository root does not automatically protect a nested package directory. Put ignore rules (or a files whitelist) next to the package.json you are publishing, then dry-run from that directory. CI matrix jobs that publish several packages should dry-run each one, not only the first.

Bundlers complicate the story. If the published artifact is a single compiled file under dist, whitelist dist and the readme, and keep sources, fixtures, and local env files out of files. If you publish TypeScript source for consumers to compile, whitelist the source folders deliberately and still exclude secrets and private notes that happen to live beside them.

What pack includes that git never tracked

Developers sometimes keep a personal .env.local or a downloaded service-account JSON in the package folder, never committing it. Gitignore patterns may or may not cover those names. Pack does not ask git whether a file is tracked; it walks the filesystem subject to ignore and files rules [1]. Untracked secrets are the usual "but it was never in the repo" incident after a publish.

Opposite failure: a required dist output exists only after npm run build. A clean CI publish job that forgets the build step packs an incomplete tarball. Dry-run in the same job sequence as publish, after the build, so the contents list matches what you ship.

Team habits that stick

Add a short CONTRIBUTING note: "Never add .npmignore without copying secret patterns from .gitignore, or prefer files." Put the dry-run grep in the publish workflow. On private packages, still avoid shipping customer data or internal URLs that do not belong in the tarball; private registries get compromised too.

When you inherit a package that already has both ignore files, do not delete .npmignore casually to "simplify" without a dry-run. Deleting it re-enables .gitignore for pack, which may suddenly exclude dist you needed or include paths you did not expect if the two files had diverged [1].

Incident response if a secret already shipped

Unpublish is limited on the public registry and does not erase copies people already downloaded. Rotate every credential that appeared in the tarball, revoke npm tokens that could republish, and ship a new version that removes the secret from the packed files. Check whether the secret also lived in git history. Tell consumers to upgrade and to treat the old version as compromised. Then add the dry-run gate so the next release cannot repeat the same ignore mistake.

If the package is scoped and private, still rotate: private packages leak through misconfigured tokens and mirrored caches. The ignore bug is independent of public versus private visibility.

The git ignore file protects the repository. The npm ignore file (or files whitelist) protects the registry tarball. They answer different questions. When both exist, only one of them answers npm [1].

Slow new registry versions with package manager release cooldown. Browse the Dev tools topic.

Send this to the friend who added an .npmignore and might be shipping their .env.

Two thin filter membranes in front of a package box about to be published, with the front membrane slightly porous so a glowing secret token slips into the public package, while the rear membrane sits unused and dark, with no typography or filenames.
If a publish ignore file exists, it is used alone. The other ignore file is not merged.

Keep this guide

By Full Byte Developer

Filed under Dev tools, Security

Questions about this guide

Which file wins?

If .npmignore exists, npm uses only .npmignore and ignores .gitignore entirely. If .npmignore is missing, npm uses .gitignore.

How do I whitelist files instead?

Use the files array in package.json. Only listed paths are included, subject to ignore rules, and always-included files such as package.json still ship.

How do I see the tarball before publish?

Run npm pack --dry-run and read the Tarball Contents list. Fix surprises before npm publish.

Sources

Next in this stack