Resolve package-lock.json Merge Conflicts Between Agents
Prepared and checked using AI. Sources are linked in the article.
To resolve a package-lock.json merge conflict after parallel agent branches, decide what belongs in package.json first, then run npm install --package-lock-only to generate a merged lockfile. Review the resulting dependency changes and run a clean install before completing the merge. A lockfile records a resolved dependency tree; accepting one agent’s entire file can discard the other agent’s changes.
Understand what each branch changed
Start in the worktree where you will combine the branches. For example, suppose agent/ui and agent/api branched from main, and you have an integration branch in a separate worktree. Keep that worktree’s unrelated changes committed or set aside: Git warns that a merge with uncommitted changes can be difficult to undo after a conflict.
Compare each branch with its common ancestor before merging. Include the manifest and lockfile so you can distinguish a deliberate dependency change from lockfile churn:
git diff main...agent/ui -- package.json package-lock.json
git diff main...agent/api -- package.json package-lock.json
Read package.json first. Did one agent add a runtime dependency while the other added a development dependency? Did both change the permitted range for the same package? Did either remove a dependency that the other branch still needs? Record the intended result before resolving any conflict markers. If you want a broader process for combining agent work, see merging parallel AI agent branches.
A large lockfile diff does not necessarily mean an agent requested many new direct dependencies. The lockfile contains the resolved package tree, including entries for packages pulled in by other packages. Review those changes, but make the direct dependency decision in the manifest.
Merge the manifests, then regenerate the lockfile
Merge one agent branch into the integration branch, resolve anything it conflicts with, and complete that merge. Then merge the second branch. If the first merge completes cleanly, the sequence is:
git merge agent/ui
git merge agent/api
git status --short
git status --short shows the changed and unmerged paths. A conflict in package-lock.json is expected when both branches changed overlapping portions of the dependency tree. A conflict in package.json needs a human decision: edit the file so its dependency ranges, sections, scripts, and other changed fields express the combined intent. Do the same for any other source conflicts before validating the combined work.
Now run this command from the directory containing the relevant package.json and package-lock.json:
npm install --package-lock-only
The --package-lock-only flag updates the lockfile without installing into node_modules. npm can use the resolved manifest and both sides of a conflicted lockfile to construct the merged tree. Inspect its output; a completed command is the start of review, not proof that both agents’ intended versions survived.
If the branches changed the same dependency to different ranges, choose the range the combined code should support. npm cannot make that product or compatibility decision for you. If the branches changed different direct dependencies, confirm both entries appear in the final manifest and in the regenerated lockfile. An automatic Git merge of package.json may still combine lines in a way that does not match the intended design.
Review the result before completing the merge
First, compare the regenerated files with the integration branch. Focus on the direct dependencies, removed packages, unexpected version changes, and changes to registry locations or integrity fields. Also inspect changes to application code that depend on the new packages. For a repeatable approach to that wider review, see reviewing AI-generated code.
git diff HEAD -- package.json package-lock.json
git diff --check
git diff --check flags added conflict markers and whitespace errors. It does not establish that the dependency tree is correct. If the lockfile diff is unexpectedly broad, check whether the branches used different npm versions or lockfile settings. The lockfile-version setting controls the lockfile format; changing formats during a merge can obscure the dependency changes you meant to review.
Next, verify that the committed files can drive a fresh install:
npm ci
npm test
npm ci fails if the lockfile and package.json disagree, removes an existing node_modules directory, and leaves both manifest files unchanged. It is therefore a useful check after regeneration, but it will replace the current installed dependency tree. npm test runs the project’s configured test script; use the project’s other relevant checks as well, especially where each agent changed code that imports a new package.
If a transitive package changed unexpectedly, run npm explain with that package name after the clean install. It shows why an installed package is present, which can help you trace a surprising version or duplicate back to its requesting dependency.
Once the manifest, lockfile, and code pass review, stage the resolved files and finish the interrupted merge:
git add package.json package-lock.json
git diff --cached --check
git diff --cached -- package.json package-lock.json
git merge --continue
Include any other files you resolved in git add. The staged diff is the version that will enter the merge commit, so check it after staging. If you need to abandon the merge, git merge --abort attempts to restore the state from before it began; this is another reason to start with a clean worktree.
If npm cannot merge the conflicted lockfile
Do not hand-edit a large set of lockfile entries until the JSON looks plausible. The entries describe relationships between packages, not just a list of names. First confirm that package.json has no conflict markers and that you ran npm at the correct package root. If npm still cannot process the conflicted lockfile, use one side as a starting point and regenerate from the resolved manifest:
git checkout --ours -- package-lock.json
npm install --package-lock-only
git checkout --ours takes the current branch’s version of that file. In a merge of agent/api into an integration branch that already contains agent/ui, this starts from the integration side. You can choose --theirs instead if the incoming branch is the better starting point. Either choice discards the other side’s lockfile-only decisions, so compare the final diff with both agent branches. This fallback is especially consequential when a branch updated a transitive dependency without changing package.json: the resolved manifest alone cannot express that specific lockfile choice.
If regeneration reports a dependency resolution error, return to the manifest decision. Check whether the chosen ranges can coexist and whether the project uses npm settings that affect the dependency tree. In particular, a project that generated its lockfile with legacy-peer-deps or install-links needs the same settings for npm ci. Keep any intentional project setting in its .npmrc so subsequent installs use it consistently.
Reduce future conflicts
Give parallel agents separate worktrees and clear ownership of dependency changes. When both tasks need package changes, ask each agent to state which direct dependencies it added, removed, or upgraded and why. That makes the manifest decision easier when you combine their branches; splitting work across agents covers the broader task boundary.
Keep the project’s npm and lockfile format choices consistent across branches. Review package.json alongside package-lock.json in each agent’s diff, and merge dependency changes in a dedicated integration worktree. After each merge, regenerate and validate the lockfile before starting the next one. Smaller, reviewed merges leave fewer simultaneous decisions in the final conflict.
Where Parallel Code fits
Parallel Code is our free, open-source desktop app for macOS and Linux. It runs AI coding agents in parallel, each in its own git worktree, and provides a diff-first review surface. Those separate worktrees let agents make isolated dependency changes; you still decide how to combine their manifests and verify the resulting lockfile.
Frequently asked questions
Should I choose ours or theirs for package-lock.json?
Use either side only as a starting point when npm cannot merge the conflicted file. Regenerate from the resolved package.json, then check what the discarded side changed; a lockfile-only update may otherwise disappear.
Can I resolve the conflict by deleting package-lock.json?
Regenerating from only package.json gives npm fewer existing version choices to preserve. Prefer merging the conflicted lockfile first, then inspect the result; if you must start over, review the entire new dependency tree and run npm ci before committing.
Why does npm ci fail after the conflict markers are gone?
Removing markers only makes the files parseable. npm ci also requires the lockfile to agree with package.json; fix the manifest decision, regenerate the lockfile, and retry with any project settings used to create it.
What if Git reports no conflict, but both agents changed dependencies?
Review both branches’ manifest changes anyway. Git can combine separate lines without knowing whether the chosen dependency ranges suit the combined code; regenerate the lockfile, inspect its diff, and validate it with a clean install and the project’s checks.