github/gitignore · Node.gitignore
Node.js .gitignore テンプレート解説
Node.gitignore が node_modules、ログ、ビルド成果物、.env、各種ツールのキャッシュを無視する理由をルールごとに説明し、lockfile・Yarn Berry の注意点をまとめます。
Node.gitignore は npm・Yarn・pnpm で依存関係をインストールする JavaScript/TypeScript プロジェクト向けのテンプレートである。特定のフレームワーク 1 つではなく、Next.js、Nuxt、Gatsby、SvelteKit、VitePress、Docusaurus、Serverless などのエコシステムのツールが作るキャッシュ・ビルドフォルダーを幅広く集めている。
要点は 3 つある。インストールすれば再生成される依存関係(node_modules/)、実行中に生まれるログ・ランタイムファイル、そしてコミットすると困る環境変数ファイル(.env)である。逆に package.json と lockfile はこのテンプレートでは無視されず、コミットすべきファイルである。
ルール別の解説
| パターン | 何を、なぜ無視するか |
|---|---|
node_modules/jspm_packages/web_modules/bower_components | 依存関係ディレクトリnpm install でいつでも作り直せ、ファイル数は数万に達し、OS ごとのネイティブバイナリが混ざっているため、別の環境でそのまま使うとかえって壊れる。スラッシュのないパターンなので、モノレポのサブパッケージにある 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 キーや DB パスワードが入るファイルである。.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