145

When multiple Git branches modify the dependencies in a project that uses Yarn, it is likely to introduce a conflict in the yarn.lock file. It is not a good idea to delete and regenerate the yarn.lock file because this will probably cause several packages to be unintentionally upgraded. What is the best way to quickly resolve conflicts in this file?

4 Answers 4

310

Since Yarn 1.0 it's easy because it has built in support for this scenario.

First solve the conflict in package.json manually, then just run this:

$ yarn install

yarn install v1.0.1
info Merge conflict detected in yarn.lock and successfully merged.
[1/4] Resolving packages...

And then the conflict will be resolved and you can commit that or continue rebasing if that was what you were doing.

16
  • 1
    I don't believe this works if there are conflicts in your yarn.lock containing lines of code such as ============, >>>>>>>>>>>>>>, <<<<<<<<<<<<<. You still need to do what Christine Schlensker's answer talks about. Oct 4, 2017 at 21:23
  • 93
    @theGreenCabbage don't believe it, try it
    – Vanuan
    Oct 9, 2017 at 12:42
  • 19
    you need to fix your conflicts in package.json first then run yarn and it should handle it
    – belgac
    Oct 11, 2018 at 11:44
  • 5
    It printed out the "successfully merged" message for me but the yarn.lock still contained the merge conflicts.
    – slikts
    Nov 5, 2020 at 9:33
  • 4
    This doesn't work if there are no conflicts in package.json
    – JESii
    Jun 22, 2022 at 22:35
83

A good approach is detailed in this github discussion about the issue.

git rebase origin/master

When the first conflict arises, I checkout the yarn.lock then re-perform the installation

git checkout origin/master -- yarn.lock 
yarn install

This generates a new yarn.lock based on the origin/master version of yarn.lock, but including the changes I made to my package.json. Then it's just a matter of:

git add yarn.lock
git rebase --continue
2
  • For this and the accepted answer, I have to repeat the commands multiple times and git ends up with the following: No changes - did you forget to use 'git add'? If there is nothing left to stage, chances are that something else already introduced the same changes; you might want to skip this patch.
    – ADP
    Jun 1, 2020 at 12:09
  • My fix ended being these steps on a regular merge - It never worked on my rebase.
    – ADP
    Jun 1, 2020 at 16:03
0

If ‘yarn install’ doesn’t helped

1) git checkout <target branch>
2) git pull
3) git checkout <our branch with conflict>
4) git merge <target branch> (you will see merge conflict on yarn.lock)
5) rm yarn.lock
6) yarn
7) git add yarn.lock
8) git commit -m
9) git push
-7

this file is too long so if you need to check conflict in vscode without terminal maybe you can try a search in this file for terms like: >>>>>>>, =======, <<<<<<< or HEAD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.