Resolving package.json Version Conflicts
Fix merge conflicts in package.json version fields and dependency lists. Common when multiple branches update npm packages simultaneously.
Detailed Explanation
Package.json Conflicts
Merge conflicts in package.json are extremely common in JavaScript and TypeScript projects. They occur when two branches add, remove, or update dependencies at the same time. Because npm and yarn modify the same JSON structure, overlapping edits create conflict markers that break the JSON syntax.
Common Conflict Patterns
Version bumps happen when two branches update the same dependency to different versions:
<<<<<<< HEAD
"react": "^18.2.0",
=======
"react": "^18.3.0",
>>>>>>> feature/upgrade-deps
New dependencies conflict when both branches add entries near the same location in the alphabetically-sorted list:
<<<<<<< HEAD
"lodash": "^4.17.21",
"luxon": "^3.4.0",
=======
"lodash": "^4.17.21",
"moment": "^2.30.0",
>>>>>>> feature/add-dates
Resolution Approach
For version bumps, generally Accept Theirs (the newer version) unless there are known breaking changes. For new dependencies, the correct resolution is usually Accept Both — you want all the packages from both branches. However, watch for trailing commas and duplicate entries after merging.
Post-Resolution Validation
After resolving the conflict, always run npm install or yarn install to regenerate the lock file, and ensure the JSON is valid by formatting it with a JSON formatter.
Use Case
Two developers on your team both added new npm packages in separate feature branches. When you merge the second branch, Git reports a conflict in the dependencies section of package.json.