github/gitignore · Node.gitignore
Node.js .gitignore 模板详解
逐条说明 Node.gitignore 为什么忽略 node_modules、日志、构建产物、.env 和各类工具缓存,并整理 lockfile 与 Yarn Berry 的注意事项。
Node.gitignore 是面向使用 npm、Yarn、pnpm 安装依赖的 JavaScript/TypeScript 项目的模板。它并不针对某一个框架,而是广泛收集了 Next.js、Nuxt、Gatsby、SvelteKit、VitePress、Docusaurus、Serverless 等生态工具生成的缓存和构建目录。
核心有三类:安装后会重新生成的依赖(node_modules/)、运行时产生的日志与运行时文件,以及提交后会带来麻烦的环境变量文件(.env)。相反,package.json 和 lockfile 不会被此模板忽略,它们是应当提交的文件。
逐条规则详解
| 模式 | 忽略什么,为什么忽略 |
|---|---|
node_modules/jspm_packages/web_modules/bower_components | 依赖目录可以随时用 npm install 重新生成,文件数可达数万个,并且混有各操作系统专用的原生二进制文件,直接拿到其他环境使用反而会出错。由于模式中没有斜杠,monorepo 子包中的 node_modules 也会一并匹配。 |
logs*.lognpm-debug.log*yarn-debug.log*yarn-error.log*report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json | 日志与诊断报告包管理器失败时留下的调试日志,以及 Node.js 诊断报告(process.report)。其中包含执行者的环境和路径,而且每次都会重新生成,没有理由保留在历史中。 |
pids*.pid*.pid.lockcoverage*.lcov.nyc_outputlib-cov | 运行时与覆盖率产物进程 ID 文件和测试覆盖率结果。覆盖率是 CI 每次重新计算的值,放在仓库中只会增加冲突。 |
distout.next.nuxt.output.svelte-kit/build/Release*.tsbuildinfo | 构建结果打包工具和框架生成的产物。*.tsbuildinfo 是 TypeScript 增量构建信息。像 dist、out 这样末尾没有 / 的模式也会忽略同名的文件,因此如果源码中有这样命名的文件,需要把规则收窄为 /dist/ 之类的形式。 |
.env.env.*!.env.example | 环境变量文件存放 API 密钥、数据库密码的文件。.env.local、.env.production 等变体也用 .env.* 拦住,而只列出键名、不含值的 .env.example 用 ! 重新包含,让团队成员知道需要哪些变量。 |
.npm.eslintcache.stylelintcache.cache.parcel-cache.vite/.pnpm-store | 工具缓存代码检查工具、打包工具、包管理器为提速而使用的缓存。删除后会自动重建,而且每个人的内容不同,提交后会不断产生无意义的变更。 |
.pnp.*.yarn/*!.yarn/patches!.yarn/plugins!.yarn/releases!.yarn/sdks!.yarn/versions | Yarn Berry(2+)只排除 .yarn/ 里的内容(.yarn/*),再用 ! 恢复需要共享的子目录。如果用 .yarn/ 排除了目录本身,git 就不会查看其中内容,! 规则也就不起作用。 |
实践中的注意事项
- 不要忽略
package-lock.json、yarn.lock、pnpm-lock.yaml,应当提交。npm 文档也强烈建议将 lockfile 纳入版本控制。 - 如果使用 Yarn 的 Zero-Installs(提交
.yarn/cache的方式),需要删除.pnp.*规则并添加!.yarn/cache。 - 如果已经提交了
.env,仅靠忽略规则无法让它消失。用git rm --cached .env取消跟踪,并作废、重新签发已暴露的密钥。 - macOS 的
.DS_Store和编辑器设置不在此模板中,请同时选择 macOS、VisualStudioCode 模板,或放入全局 gitignore。
原始模板
Node.gitignore
# Logslogs*.lognpm-debug.log*yarn-debug.log*yarn-error.log*lerna-debug.log*# Diagnostic reports (https://nodejs.org/api/report.html)report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json# Runtime datapids*.pid*.seed*.pid.lock# Directory for instrumented libs generated by jscoverage/JSCoverlib-cov# Coverage directory used by tools like istanbulcoverage*.lcov# nyc test coverage.nyc_output# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files).grunt# Bower dependency directory (https://bower.io/)bower_components# node-waf configuration.lock-wscript# Compiled binary addons (https://nodejs.org/api/addons.html)build/Release# Dependency directoriesnode_modules/jspm_packages/# Snowpack dependency directory (https://snowpack.dev/)web_modules/# TypeScript cache*.tsbuildinfo# Optional npm cache directory.npm# Optional eslint cache.eslintcache# Optional stylelint cache.stylelintcache# Optional REPL history.node_repl_history# Output of 'npm pack'*.tgz# Yarn Integrity file.yarn-integrity# dotenv environment variable files.env.env.*!.env.example# parcel-bundler cache (https://parceljs.org/).cache.parcel-cache# Next.js build output.nextout# Nuxt.js build / generate output.nuxtdist.output# Gatsby files.cache/# Comment in the public line in if your project uses Gatsby and not Next.js# https://nextjs.org/blog/next-9-1#public-directory-support# public# vuepress build output.vuepress/dist# vuepress v2.x temp directory.temp# Sveltekit cache directory.svelte-kit/# vitepress build output**/.vitepress/dist# vitepress cache directory**/.vitepress/cache# Docusaurus cache and generated files.docusaurus# Serverless directories.serverless/# FuseBox cache.fusebox/# DynamoDB Local files.dynamodb/# Firebase cache directory.firebase/# TernJS port file.tern-port# Stores Visual Studio Code versions used for testing Visual Studio Code extensions.vscode-test# pnpm.pnpm-store# yarn v3.pnp.*.yarn/*!.yarn/patches!.yarn/plugins!.yarn/releases!.yarn/sdks!.yarn/versions# Vite filesvite.config.js.timestamp-*vite.config.ts.timestamp-*.vite/
模板来源: github/gitignore/Node.gitignore @356fd7b (2026-09-11) · CC0-1.0