How to Fix package-lock.json Merge Conflict
In this tutorial, you'll learn about How to Fix package. We cover key concepts, practical examples, and best practices.
The Problem
Git merge conflicts in package-lock.json occur when multiple branches install different versions of packages, creating merge conflicts that are difficult to resolve manually.
Quick Fix
Step 1: Accept one version and regenerate
The simplest approach is to accept either side and re-run install:
git checkout --ours package-lock.json
npm install
Or accept the other side:
git checkout --theirs package-lock.json
npm install
This regenerates a consistent lock file.
Step 2: Resolve manually
If you need to manually merge:
{
"dependencies": {
"lodash": {
<<<<<<< HEAD
"version": "4.17.20",
=======
"version": "4.17.21",
>>>>>>> branch
}
}
}
Keep the correct version and run npm install after.
Step 3: Use npm-merge-driver
Install a dedicated merge driver:
npx npm-merge-driver install --global
This automatically resolves package-lock.json conflicts, generating a merged lock file that satisfies all dependency requirements.
Step 4: Regenerate from scratch
Delete and regenerate:
rm package-lock.json
npm install
This creates a fresh lock file based on current package.json requirements.
Step 5: Use yarn or pnpm to avoid lock file conflicts
Consider switching to Yarn or pnpm, which have better merge conflict resolution for lock files:
yarn import # Convert from npm to yarn
Prevention
- Run
npm installafter every version bump to keep lock file updated. - Use
npm-merge-driverin team repos. - Keep dependency updates in separate branches with fewer changes.
- Use
npm ciin CI instead ofnpm install. - Avoid manually editing
package-lock.json.
Common Mistakes with package lock conflict
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
These mistakes appear frequently in real-world NODE code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro