skip to content

With npm, how should you resolve a Git merge conflict in `package-lock.json` after two branches each added dependencies?

level: middleimportance: must knowfreq 24%

answer

  1. generated files are not hand-edited
  2. manifest first, lock second
  3. npm will not parse a conflicted manifest
  4. one side's lock, then a plain install
  5. npm ci proves the result

basics

~20 s

Resolve package.json by hand first, then take one side's package-lock.json whole and run npm install, which keeps every locked version that still satisfies the merged ranges and resolves the rest. Commit both files and confirm with npm ci.

solid answer

~40 s

Don't hand-merge the lock. First fix `package.json`: that is the real merge, both branches' new dependencies and ranges, and npm will not even parse a `package.json` that still has conflict markers (it reports "Merge conflict detected in your package.json"). Then check out one side's `package-lock.json` unchanged, usually the target branch's, and run `npm install`. npm compares that lock with the merged manifest: every locked version that still satisfies its range is kept, and anything missing or out of range is resolved and written into the lock. Run `npm ci` to prove the two files agree, then commit both. Deleting the lock instead would re-resolve the entire tree and quietly move every transitive package.

code

bash · 6 lines
bash
# merging feature/fare-zones into main; package.json conflict already fixed by hand
git checkout --ours package-lock.json   # during a merge, --ours is main's lock
npm install                             # reconcile the lock with the merged package.json
npm ci                                  # lock and manifest agree, clean install works
git add package.json package-lock.json
git commit                              # conclude the merge

go deeper

for a junior

Remember the rule: never hand-edit package-lock.json. Fix package.json, take one side's lock unchanged, run npm install and commit both files.

for a middle

Explain why: lock entries are keyed by their node_modules path, so placements depend on each other, and npm install keeps every locked version that still satisfies the merged ranges.

for a senior

Show that you review the regenerated diff: new entries, unexpected resolved hosts, new install-script flags, and a passing npm ci before the merge lands.

for a principal

Look at reducing these conflicts structurally: smaller dependency changes per pull request, one owner for bulk upgrades, and a merge check that runs npm ci on the merged result.

## What a conflicted lock actually contains `package-lock.json` is a generated description of one exact `node_modules` tree. Since npm 9 it is written as `lockfileVersion: 3` (npm 7 and 8 wrote `2`), and its core is the `packages` object, keyed by **location in the tree**: | Lock field | What it records | |---|---| | key `""` | the root project, mirroring the dependency fields of `package.json` | | key `node_modules/chrono-fmt` | a package placed at that path; a nested copy appears as `node_modules/route-map-kit/node_modules/chrono-fmt` | | `version` | the exact version installed at that location | | `resolved` | where it came from, usually a registry tarball URL | | `integrity` | an SRI `sha512` (or older `sha1`) string for that tarball | | `dev`, `optional`, `devOptional` | which dependency sections it is reachable from | | `hasInstallScript` | set when the package has a `preinstall`, `install` or `postinstall` script | When two branches each run `npm install` for a different package, both rewrite the root entry and both add or move entries, often next to each other alphabetically. Git sees overlapping text edits and marks a conflict. ## Why hand-merging the lock goes wrong - **Placements depend on each other.** Whether a package sits at `node_modules/x` or nested under a dependent depends on every other range in the tree; splicing two halves can describe a layout npm would never build. - **The root entry is derived.** It has to match the merged `package.json` exactly; hand-picking one side's root entry leaves the other branch's dependency out. - **Fields travel together.** A `version` from one side with a `resolved` URL or `integrity` hash from the other describes no real tarball. - **The damage can be silent.** The result can be valid JSON yet disagree with `package.json`, and you discover it when `npm ci` fails in CI or, worse, when it installs a tree nobody reviewed. ## The procedure 1. **Resolve `package.json` by hand.** Keep both branches' additions and agree on any range both changed. npm refuses a `package.json` with conflict markers, so this step cannot be skipped. 2. **Take one side's `package-lock.json` whole**, normally the target branch's, without editing it. 3. **Run `npm install` with no arguments.** npm compares the lock with the merged manifest: versions that still satisfy their ranges are kept exactly; dependencies only the other branch added are resolved again within their ranges and written in. Use `npm install --package-lock-only` if you only need the file. 4. **Run `npm ci`** locally or let CI run it. It proves the lock and `package.json` agree and that the tree installs from scratch. 5. **Commit both files** to finish the merge. One consequence to know: the other branch's new dependencies are re-resolved, so they can land on a newer version inside their range than the one that branch had locked. Run the tests after the merge, not only before it. ## What not to do - **Do not keep both halves** of every conflict hunk; duplicate keys and mismatched fields follow. - **Do not delete the lock** and reinstall. Without a lock npm resolves every range from scratch, so every transitive package moves to the newest version allowed that day: a merge that should add two packages becomes an unreviewed upgrade of the whole tree. - **Do not run `npm update`** to settle it; that deliberately moves packages to newer in-range versions, which is a separate change with its own review. ## Reviewing the regenerated diff Read the lock diff like code. Expect new `packages` entries for each added dependency and its own dependencies, each with a `resolved` URL and an `integrity` hash. Look twice at a `resolved` URL on an unexpected host and at any new entry flagged `hasInstallScript`, since that package wants to run code at install time. A small, explainable diff is the sign the merge went right. A quick checklist before pushing the merge commit: - `package.json` has no conflict markers and contains both branches' additions. - `npm ci` completes without the out-of-sync error. - `npm ls` prints no `invalid` or `UNMET DEPENDENCY` lines. - The lock diff touches the packages the two branches added, and little else. If the diff is much larger than the two branches' changes, something re-resolved more than intended, usually because the lock was deleted or a range was widened while resolving `package.json`; start again from one side's lock.

  • Why is deleting `package-lock.json` and reinstalling a bad way out of the conflict?
    Without a lock, npm resolves every range from scratch, so every direct and transitive dependency moves to the newest version its range allows on that day. A merge that should add two packages becomes an upgrade of the whole tree, and the diff is too large for anyone to review honestly.
  • What does the `""` key in the lock's `packages` object hold, and why does it conflict?
    It is the root project: its name, version and the dependency fields from `package.json`. Both branches changed those dependencies, so the root entry conflicts too. Regenerating from the merged `package.json` rewrites it consistently instead of picking one branch's list by hand.
  • How can you refresh only the lock during the merge, without reinstalling `node_modules`?
    `npm install --package-lock-only` resolves against the merged `package.json` and writes `package-lock.json` without downloading into `node_modules`. It suits a quick merge on a machine that does not need a working install; run `npm ci` afterwards wherever the code is built.

  • Keep both sides of every conflict hunk in the lock and it will be fine.
  • Delete package-lock.json and reinstall; a fresh lock has no downside.
  • Resolve the lockfile first, then sort out package.json afterwards.
  • A hand-merged lock is fine because npm ci repairs it on the next run.
  • Run npm update to settle the lockfile conflict.