Git Branch Dependency Issues - Quick Fix Guide
Common Symptoms
Module not found: Can't resolve 'package-name'npm error code ERESOLVEwith peer dependency conflicts- App fails to start after switching branches
- Dependencies seem to be missing or wrong versions
Root Cause
When switching Git branches, your node_modules and lock files may be out of sync with the current branch's package.json. Different branches often have different dependency versions.
Quick Fix Steps
1. Identify Your Package Manager
Check which lock file exists in your project:
package-lock.json→ use npmyarn.lock→ use yarnpnpm-lock.yaml→ use pnpm
2. Clean Install Dependencies
For npm projects:
bash
rm -rf node_modules package-lock.json
npm installFor yarn projects:
bash
rm -rf node_modules yarn.lock
yarn installFor pnpm projects:
bash
rm -rf node_modules pnpm-lock.yaml
pnpm install3. If Still Getting Errors
npm:
bash
npm install --legacy-peer-depsyarn:
bash
yarn install --ignore-enginesBest Practice: Branch Switching Routine
Switch branch:
bashgit checkout <branch-name>Check if package.json changed:
bashgit diff HEAD~1 package.jsonIf changed, clean install:
bash# Choose based on your lock file rm -rf node_modules [package-lock.json|yarn.lock|pnpm-lock.yaml] [npm|yarn|pnpm] installStart development:
bash[npm run dev|yarn dev|pnpm dev]
Pro Tips
- Always use the same package manager across your team
- Consider adding
node_modules/to.gitignoreif not already there - Use
npm cioryarn install --frozen-lockfilefor production builds - Keep lock files in version control
When This Happens Most
- Switching between feature branches with different dependencies
- Merging branches that updated package.json
- Switching between branches from different time periods
- Working with multiple developers who use different package managers