How to Create a Node.js .gitignore File
Best practices and examples for creating a .gitignore file tailored for Node.js, npm, and Yarn projects.
Why Node.js needs specific ignore rules
Node.js projects generate massive dependency folders (node_modules), environment files (.env), and debug logs. Committing these can bloat your repository and expose secrets.
Key entries to include
Every Node.js project must ignore node_modules/, npm-debug.log, yarn-error.log, and coverage/. If you use TypeScript, also ignore your compiled output directory (e.g., dist/ or build/).
Environment Variables
Always ignore .env, .env.local, and .env.* (except .env.example) to ensure API keys and database passwords are never leaked into version control.