Skip to content

How to Fix package-lock.json Merge Conflict

DodaTech Updated 2026-06-24 2 min read

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 install after every version bump to keep lock file updated.
  • Use npm-merge-driver in team repos.
  • Keep dependency updates in separate branches with fewer changes.
  • Use npm ci in CI instead of npm install.
  • Avoid manually editing package-lock.json.

Common Mistakes with package lock conflict

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. 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

### Should I commit package-lock.json?

Yes. Since npm 5.7+, package-lock.json should be committed to version control. It ensures reproducible installs across environments. Ignoring it defeats the purpose of deterministic builds. The only exception is libraries used as dependencies, where the lock file is not utilized by consumers.

Why does package-lock.json conflict so often?

Each branch may install different package versions or add new packages. When merged, the lock file has two different states for the same dependency tree. The lock file's format (nested metadata per package) makes it prone to conflicts. Using a merge driver or frequent rebasing helps.

Can I use git attributes to handle lock file conflicts?

Yes. Add to .gitattributes:

package-lock.json merge=npm-merge-driver

This requires the npm-merge-driver tool to be installed. Alternatively, mark the lock file as binary to prevent merging:

package-lock.json -diff -merge

But this means you must regenerate after every merge operation.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro