In an npm package that nearly published a `.env` and its source maps, how do `files` and `.npmignore` decide what `npm publish` includes?
answer
- allow-list against deny-list
- one ignore file at a time
- .npmignore displaces .gitignore
- root .npmignore cannot beat files
- .env is on no protected list
basics
~20 sfiles is an allow-list: only matching paths, plus package.json, README, LICENSE, main and bin, are packed. .npmignore is a deny-list over the rest, and once it exists npm stops reading .gitignore, so a git-ignored .env can ship.
solid answer
~40 s`files` in `package.json` is an **allow-list**: when it is set, only the paths and globs it names are packed, plus the always-included `package.json`, README, LICENSE, `main` and `bin` files. `.npmignore` is a **deny-list** with `.gitignore` syntax, and npm uses only one of the two ignore files in a directory: if a `.npmignore` exists, the `.gitignore` there is ignored. That is the usual near miss — someone adds a `.npmignore` containing `*.map` to drop source maps, and the `.env` that only `.gitignore` excluded is packed again, because npm protects `.npmrc` but not `.env`. Also, a `.npmignore` at the package root does not override `files`; one in a subdirectory does. The robust fix is a narrow `files` list such as `["dist/**/*.js", "dist/**/*.css"]`, checked with `npm pack --dry-run` before each release.
code
json · 9 lines{
"name": "a11y-datepicker",
"version": "2.4.2",
"main": "dist/index.js",
"files": [
"dist/**/*.js",
"dist/**/*.css"
]
}go deeper
Remember the two directions: files says what to include, .npmignore says what to leave out, and only one ignore file is read per directory.
Walk through the precedence: files first, then the single active ignore file, the root exception, and the fixed always-in and never-in lists.
Diagnose the near miss: a new .npmignore displaced .gitignore, so .env shipped; fix it with an allow-list and a dry-run check in the release job.
Make fail-closed publishing the team default: allow-lists for every package and an automated tarball review, rather than per-repo ignore files nobody audits.
## Two mechanisms pointing in opposite directions npm decides what goes into a published tarball with two tools that work in opposite directions: - The **`files` field** in `package.json` is an **allow-list**. It names the paths and glob patterns to include (`"dist"`, `"dist/**/*.js"`); a directory is walked recursively. When the field is absent, npm treats it as `["*"]` — everything. - A **`.npmignore` file** is a **deny-list**. It uses the same pattern rules as `.gitignore`, including `!` to negate a pattern, and removes matching files from whatever would otherwise ship. On top of both sit two fixed lists. `package.json`, the README, the LICENSE (or LICENCE), the file named in `main` and the files in `bin` are **always included**. `.git`, `.npmrc`, `node_modules` (bundled dependencies aside) and the lockfiles — `package-lock.json`, `pnpm-lock.yaml`, `yarn.lock`, `bun.lockb` — are **never included**. A `.env` file is on neither list. npm has no idea it holds secrets; it is an ordinary dotfile. ## How npm combines them For a given directory, npm effectively works through these steps: 1. Start from `files` — the listed paths, or everything when the field is absent. 2. Pick **one** ignore file for that directory: the `.npmignore` if it exists, otherwise the `.gitignore`. Never both. 3. Apply that ignore file, with one exception: at the **package root**, `.npmignore` does **not** override `files`. In a **subdirectory**, a `.npmignore` does override it. 4. Add the always-included files and remove the never-included ones, along with npm's built-in ignores (such as `.DS_Store` and `npm-debug.log`) unless `files` names them explicitly. The result is what `npm pack --dry-run` prints under "Tarball Contents". Two consequences follow. With `"files": ["dist"]`, every file under `dist/` ships — compiled code, type files, source maps, a stray `dist/.env` written by a careless build script — unless a `.npmignore` **inside** `dist/` removes it, because a directory in `files` is walked recursively and only a subdirectory ignore file can trim it. And with no `files` field at all, the ignore file is the only thing standing between the folder and the registry, so whichever ignore file is active must carry every secret pattern itself. ## The trap: adding a `.npmignore` switches off `.gitignore` The near miss in this question is the most common one. The project had no `files` field, so its `.gitignore` was doing double duty: it kept `.env` out of Git **and**, by fallback, out of the tarball. Then a maintainer wanted to stop shipping source maps and created a `.npmignore` with a single line: ``` *.map ``` From that moment npm reads `.npmignore` instead of `.gitignore` at the root. The `.env` is no longer excluded by anything, and the next `npm publish` packs it. Even an **empty** `.npmignore` has this effect — the npm docs suggest it as the way to publish something your `.gitignore` excludes. The deny-list approach has a structural weakness: every new file in the folder ships until someone remembers to deny it. ## Keeping secrets and source maps out | Approach | What ships | Risk | |---|---|---| | No `files`, rely on `.gitignore` | Everything Git ignores stays out | Breaks the day a `.npmignore` appears | | No `files`, a `.npmignore` deny-list | Everything not denied | Every new file ships by default; must repeat the `.gitignore` secrets | | `files` allow-list, e.g. `["dist/**/*.js", "dist/**/*.css"]` | Only the listed output plus the always-included files | A forgotten output path is missing, which a dry run shows at once | | `files: ["dist"]` with maps built into `dist/` | All of `dist/`, maps included | A root `.npmignore` cannot remove them; narrow the globs or keep maps out of `dist/` | An allow-list fails **closed**: if you forget something, the package is missing a file, which a consumer or a smoke test notices. A deny-list fails **open**: if you forget something, a secret leaves the building, and nobody notices until it is public. ## Checking the result Whichever mechanism you choose, verify it the same way: - Run `npm pack --dry-run` and read the file list, looking for dotfiles, `*.map`, `src/`, tests and fixtures. - Run it again after any change to `files`, `.npmignore`, `.gitignore` or the build output folder. - Put the same command in the release job and fail the job when an unexpected path appears. - Remember that `.npmrc` is the only credential-shaped file npm itself refuses to pack; tokens anywhere else are your responsibility. If a secret has already been published, removing the file from the next version is not enough — the credential has to be treated as leaked and rotated.
- An npm package has `"files": ["dist"]` and a root `.npmignore` listing `dist/*.map`. Do the source maps ship?Yes. At the package root a `.npmignore` does not override `files`, so everything under `dist/` is packed. A `.npmignore` placed inside `dist/` would override it, because in subdirectories it does; narrowing the `files` globs to the JavaScript and CSS you mean to ship is the clearer fix.
- Why doesn't npm refuse to pack `.env` the way it refuses `.npmrc`?npm's never-included list is fixed: `.git`, `.npmrc`, `node_modules` and the lockfiles. It exists to protect npm's own configuration and install state, not to recognise secrets by name, so `.env`, key files and credentials in any other file depend entirely on `files` or the active ignore file.
- A .npmignore adds its rules on top of the .gitignore rules
- A root .npmignore can remove paths that the files field lists
- npm never publishes dotfiles such as .env
- Once files is set, README and LICENSE must be listed or they are dropped
- An empty .npmignore changes nothing when a .gitignore exists