.gitignore for React Projects
Optimized .gitignore for React apps built with Create React App, Vite, or custom webpack. Covers build folders, caches, and environment variable files.
Detailed Explanation
React projects inherit most patterns from Node.js but have additional framework-specific directories and build artifacts to exclude. The right .gitignore depends on your build tool.
Create React App (CRA):
build/— The production build output fromnpm run build. This directory contains minified, hashed JavaScript and CSS bundles. It is fully reproducible and should be built by CI, not committed.node_modules/— As with all Node.js projects, never commit dependencies.
Vite-based React projects:
dist/— Vite outputs the production build todist/by default instead ofbuild/..vite/— Vite's dependency pre-bundling cache directory.
Common React patterns:
.env.local,.env.development.local,.env.production.local— Environment overrides containing API keys or feature flags. CRA and Vite both load these automatically. The base.envfile is typically committed with non-sensitive defaults, but.env.localmust always be ignored.coverage/— Test coverage from Jest or Vitest.storybook-static/— Built Storybook documentation site. If you use Storybook for component development, ignore the static output..eslintcache— ESLint caching file that speeds up linting but is machine-specific.*.tsbuildinfo— TypeScript incremental compilation cache. Useful locally but not needed in the repo.
Monorepo considerations: If your React app lives inside a monorepo (Turborepo, Nx, Lerna), each package may have its own dist/ or build/ directory. Use patterns like **/dist/ or add a root-level .gitignore that covers all workspaces.
What to commit: Always commit package-lock.json, tsconfig.json, and .eslintrc configuration files. These ensure consistent development environments across your entire team.
Use Case
A frontend team migrating from Create React App to Vite needs to update their .gitignore to cover the new dist/ output directory while preserving existing ignore rules.