git

.gitignore for Node.js

Comprehensive .gitignore for Node.js projects covering dependencies, build output, environment files, and common editors.

Overview

A comprehensive .gitignore file for Node.js projects. Covers package manager artifacts, build output, environment variables, test coverage, editor configs, and OS-specific files. Works with npm, yarn, and pnpm.

Configuration

# .gitignore

# ── Dependencies ──
node_modules/                  # Installed packages (npm, yarn, pnpm)
.pnp.*                        # Yarn Plug'n'Play
.yarn/*                        # Yarn v2+ cache and state
!.yarn/patches                 # Keep Yarn patches
!.yarn/plugins                 # Keep Yarn plugins
!.yarn/releases                # Keep Yarn releases
!.yarn/sdks                    # Keep Yarn editor SDKs

# ── Build Output ──
dist/                          # Compiled output
build/                         # Build artifacts
out/                           # Next.js output
.next/                         # Next.js build cache
.nuxt/                         # Nuxt.js build cache
.output/                       # Nitro/Nuxt output
.vercel/                       # Vercel deployment config
.netlify/                      # Netlify deployment config

# ── Environment Variables ──
.env                           # Default environment file
.env.local                     # Local overrides
.env.*.local                   # Environment-specific local overrides
.env.development               # Dev environment (if contains secrets)
.env.production                # Prod environment (if contains secrets)

# ── Testing ──
coverage/                      # Test coverage reports
.nyc_output/                   # NYC/Istanbul coverage data
*.lcov                         # LCOV coverage files
reports/                       # Test result reports

# ── Logs ──
logs/                          # Application logs
*.log                          # All log files
npm-debug.log*                 # npm debug logs
yarn-debug.log*                # Yarn debug logs
yarn-error.log*                # Yarn error logs
pnpm-debug.log*                # pnpm debug logs
lerna-debug.log*               # Lerna debug logs

# ── Runtime ──
pids/                          # Process ID files
*.pid                          # PID files
*.seed                         # Seed files
*.pid.lock                     # PID lock files

# ── Caches ──
.cache/                        # Generic cache directory
.parcel-cache/                 # Parcel bundler cache
.turbo/                        # Turborepo cache
.eslintcache                   # ESLint cache
.stylelintcache                # Stylelint cache
*.tsbuildinfo                  # TypeScript incremental build info

# ── OS Files ──
.DS_Store                      # macOS directory metadata
Thumbs.db                      # Windows thumbnail cache
Desktop.ini                    # Windows folder config
*~                             # Linux backup files

# ── Editor / IDE ──
.vscode/*                      # VS Code settings
!.vscode/extensions.json       # Keep recommended extensions
!.vscode/settings.json         # Keep shared settings (optional)
.idea/                         # JetBrains IDEs
*.swp                          # Vim swap files
*.swo                          # Vim swap files
*~                             # Emacs backup files

# ── Misc ──
*.tgz                          # npm pack output
.npmrc                         # npm config (may contain tokens)

Key Options Explained

  • node_modules/ — Always ignore. Contains hundreds of megabytes of dependencies that are reinstalled from package-lock.json.
  • !.yarn/patches — Yarn v2+ stores important files in .yarn/. The negation patterns (!) keep files needed for reproducible installs.
  • .env files — Environment files often contain API keys and secrets. Track .env.example instead with placeholder values.
  • *.tsbuildinfo — TypeScript incremental compilation cache. Machine-specific and regenerated automatically.
  • !.vscode/extensions.json — Sharing recommended extensions helps team members set up their editor. Settings can be shared too if the team agrees.

Common Modifications

  • Track .env.example: Create a .env.example with dummy values and remove it from .gitignore so new developers know what variables to set.
  • Monorepo: Add patterns for workspace-specific output like packages/*/dist/ and apps/*/.next/.
  • Storybook: Add .storybook-static/ for Storybook build output.
  • Prisma: Add prisma/migrations/*.sql negation if you want to track migration files but ignore generated client code.
  • Secrets detection: Use git-secrets or gitleaks as a pre-commit hook to catch accidentally committed secrets.