npm .gitignore template
The npm template ignores the dependency directory, debug logs and the local cache, and also ignores package-lock.json, which almost every project should commit instead.
At a glance
- Category
- Build Tool
- Patterns in this template
- 6 rules
- Directories ignored
- 1 of the 6 rules end in a slash, so they match directories only
- Also needs an OS template
- Yes. This template covers the package manager and nothing else.
- Typical companions
- JavaScript or Node.js, plus an OS template and an editor template.
In detail
The package-lock.json rule is the one to change. The lockfile records the exact resolved version and integrity hash of every package in the tree, including transitive dependencies that your manifest never names. It is what makes an install today produce the same tree as an install in six months, and it is what a security audit reads to tell you which exact versions you are running.
Ignoring it means every install resolves fresh against the registry within your declared version ranges. A patch release published overnight silently enters your build, CI tests something slightly different from what you tested locally, and reproducing a bug report becomes guesswork. The current guidance from the tool's own documentation is to commit it, and the only real exception is a published library whose consumers resolve their own tree.
node_modules is the uncontroversial half. It is the installed tree, restored from the manifest and the lockfile, and it is the largest directory in most JavaScript projects by a wide margin. Packages with native components compile during install, so a committed tree is wrong for anyone on a different platform.
npm-debug.log and its numbered variants are written when an install or a script fails. They contain the full command line, the resolved dependency tree at the point of failure and the environment, which makes them useful for one debugging session and useless afterwards.
.npm is the cache directory when the tool is configured to keep it inside the project rather than in the home directory. .eslintcache is a linter cache that stores per file results keyed by content hash, so a rerun only checks what changed.
Examples
node_modules/
npm-debug.log
npm-debug.log.*
package-lock.json
.npm
.eslintcache6 lines exactly as the tool writes them, before any common additions you tick.
Things that catch people out
- Remove the package-lock.json rule unless you are publishing a library. Ignoring the lockfile means CI and production can resolve different versions than you tested.
- This template does not ignore .npmrc, and a project level .npmrc is where a registry auth token lands. Add it, or keep the token in your home directory instead.
- node_modules/ is unanchored, so nested workspace package directories are covered too, which is what a monorepo wants.
Try it: .gitignore Generator
Pick one or more templates, tick the common additions you want, and copy or download the combined .gitignore.
.gitignore Generator
LOCAL · Browser-OnlyGenerate comprehensive .gitignore files by combining templates for languages, frameworks, IDEs, and operating systems
Language
Framework
IDE
OS
Build Tool
Infrastructure
Common Additions
Preview
Select templates or common additions to generate a .gitignore file.
Related .gitignore templates
All .gitignore templates →Yarn .gitignore template
The Yarn template ignores the .yarn directory's contents while negating five subdirectories, because which ones you keep depends on the install mode you use.
pnpm .gitignore template
The shortest package manager template, because pnpm keeps its content addressed store outside the project and links into it rather than copying packages.
Node.js .gitignore template
The longest JavaScript oriented template: dependency directories, every package manager's debug log, bundler caches, env files and Yarn Plug'n'Play state.
Docker .gitignore template
A small template with two rules worth questioning: it ignores .dockerignore, and it ignores every compose file except two, which suits few projects.
Terraform .gitignore template
State files contain secrets in plain text and must never be committed. This template covers that, and also ignores the lock file, which should be tracked.
Vagrant .gitignore template
Two rules worth understanding: the .vagrant directory holds machine identifiers and a generated private key, and box files are multi gigabyte disk images.
Tools for this
- .gitignore Generator Generate .gitignore files for any project - 50+ templates for languages, frameworks, IDEs, and OS
- package.json Generator Generate package.json files with a form-based builder - 8 project presets, all fields supported