150

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?

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Christian Schlensker
  • 21,708
  • 19
  • 73
  • 121

4 Answers4

325

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.

OZZIE
  • 6,609
  • 7
  • 55
  • 59
Vanuan
  • 31,770
  • 10
  • 98
  • 102
  • 2
    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. – theGreenCabbage Oct 04 '17 at 21:23
  • 96
    @theGreenCabbage don't believe it, try it – Vanuan Oct 09 '17 at 12:42
  • 1
    Doesn't work, throws `error An unexpected error occurred: "Unknown token 7713:1 in /location` – Saras Arya Feb 02 '18 at 07:50
  • Works for me `16:23 $ yarn` `yarn install v1.7.0` `info Merge conflict detected in yarn.lock and successfully merged.` – Brian Di Palma Jun 25 '18 at 15:23
  • 20
    you need to fix your conflicts in package.json first then run yarn and it should handle it – belgac Oct 11 '18 at 11:44
  • Worked liked charm. – Chukwuemeka Inya Feb 19 '19 at 16:58
  • 5
    It printed out the "successfully merged" message for me but the yarn.lock still contained the merge conflicts. – slikts Nov 05 '20 at 09:33
  • 2
    this works but webstorm shows still as it is conflicted, how to mark it as resolved? – rsobies Jul 15 '21 at 13:24
  • 1
    This did not work for me with yarn 1.22.17. As @theGreenCabbage says the conflicts still remain in the yarn.lock. There are no conflicts in my package.json. – Erik Pukinskis Dec 23 '21 at 00:53
  • 2
    @rsobies just run in terminal `git add yarn.lock` – frutality Jan 22 '22 at 07:01
  • delete yarn.lock and do a freah yarn install with packages mentioned in package.json – Hussam Khatib May 29 '22 at 17:13
  • 4
    This doesn't work if there are no conflicts in `package.json` – JESii Jun 22 '22 at 22:35
  • @JESii If you have a conflict in yarn.lock without a change in package.json there's something wrong in your workflow. You shouldn't touch yarn.lock without changing the version in package.json – Vanuan Sep 07 '22 at 12:39
  • @vanuan it can happen if you are cherry-picking or rebasing several version updates to packages that are not on adjacent lines in package.json (so git easily merges them) but they both mess with deeper dependencies that conflict in yarn.lock, which git can't natively resolve. – Brad P. Sep 30 '22 at 09:41
  • For those using rails, `yarn install --check-files` did fix the merge conflicts, but I'd still get an error saying my yarn packages were out of date. Deleting the `node_modules` folder and then running `yarn install --check-files` fixes things. – tquill Jan 03 '23 at 19:37
  • This doesn't work even if you do have conflicts in `package.json`. It just complains that `package.json` has invalid JSON due to `<<<`/`>>>`/`===`. – Slbox Feb 22 '23 at 23:08
  • @Vanuan lockfile contents are not deterministic from `package.json` contents. The same `package.json` can generate two different lockfiles if you run the install at different times. – Matthias Apr 19 '23 at 20:18
87

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
dhilt
  • 18,707
  • 8
  • 70
  • 85
Christian Schlensker
  • 21,708
  • 19
  • 73
  • 121
  • 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 01 '20 at 12:09
  • My fix ended being these steps on a regular merge - It never worked on my rebase. – ADP Jun 01 '20 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
  • i'd be careful with deleting the lock file. this will throw away any locked versions and you will most likely end up with versions of packages you have never tested in integration before. – Alex Spieslechner Jun 12 '23 at 06:52
-8

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

Muhammed Moussa
  • 4,589
  • 33
  • 27