.gitignore for a Node.js Project
A good .gitignore prevents accidental commits of node_modules, build artifacts, environment files containing secrets, and OS-generated junk files. This example covers the full Node.js ecosystem including npm, yarn, pnpm, Next.js, Vite, and TypeScript build outputs. The gitignore generator lets you combine multiple templates (Node, macOS, Windows, JetBrains) into a single merged file. Always verify your .gitignore covers .env files before your first commit.
Example
# Dependencies node_modules/ .pnp .pnp.js # Build outputs dist/ build/ .next/ .nuxt/ out/ # Environment files .env .env.local .env.*.local # Logs *.log npm-debug.log* yarn-debug.log* # TypeScript *.tsbuildinfo # OS .DS_Store Thumbs.db # Editor .vscode/ .idea/
FAQ
- Should I commit node_modules to git?
- No. node_modules can contain thousands of files and hundreds of megabytes. Always add it to .gitignore and rely on package.json and the lock file to restore dependencies with npm ci.
- How do I ignore already-committed files?
- Adding a file to .gitignore only prevents future tracking. To stop tracking a file already in git, run git rm --cached filename and then commit the removal.
- Can I use a global gitignore for OS files?
- Yes. Configure a global ignore file with git config --global core.excludesfile ~/.gitignore_global and add .DS_Store, Thumbs.db, and editor directories there so they apply to every repository.
Related Examples
.gitignore for a Python Project
Python projects generate bytecode files, virtual environment directories, and te...
Manage a .env Environment FileEnvironment files store secrets and configuration that differ between developmen...
Dockerfile for a Node.js ApplicationA well-structured Node.js Dockerfile uses a multi-stage build to keep the final ...