207

In a team set up, usually, I have faced merge conflicts in package-lock.json and my quick fix has always been to delete the file and regenerate it with npm install. I have not seriously thought about the implication of this fix because it has not caused any perceivable problem before.

Is there a problem with deleting the file and having npm recreate it that way instead of resolving the conflicts manually?

John Mutuma
  • 3,150
  • 2
  • 18
  • 31
  • Refer this answer - https://stackoverflow.com/a/44297998/2251411 – Ashwani Agarwal Jan 10 '19 at 08:03
  • @john-mutuma Since npm has a much easier solution than the accepted answer maybe you should consider accepting that (my) solution below instead :-) Why waste time on something that can be fixed for you much easier? :-) – OZZIE Sep 07 '20 at 08:37

4 Answers4

183

Yes, it can and will affect all the project in really bad way.

  1. if your team does not run npm install after each git pull you all are using different dependencies' versions. So it ends with "but it works for me!!" and "I don't understand why my code does not work for you"

  2. even if all the team runs npm install it still does not mean everything is ok. at some moment you may find your project acts differently. in a part that you have not been changing for years. and after (probably, quite painful) debugging you will find it's because of 3rd level dependency has updated for next major version and this led some breaking changes.

Conclusion: don't ever delete package-lock.json.

Yes, for first level dependencies if we specify them without ranges (like "react": "16.12.0") we get the same versions each time we run npm install. But we cannot say the same about dependencies of 2+ level deep (dependencies that our dependencies are relying on), so package-lock.json is really important for stability.

In your case you better do next way:

  1. fix conflicts in package.json
  2. run npm install

As easy as it looks. The same to yarn - it fixes lockfile conflict on its own. The only requirement here to resolve all the conflicts in package.json beforehand if any.

Per docs npm will fix merge conflicts in package-lock.json for you.

[Upd from 2021] important! If you use some library already and npm/GitHub account of its maintainer is hacked. And new version with malicious code inside is released. And you have package-lock.json intact. You will be fine. If you drop it you are in trouble.

skyboyer
  • 22,209
  • 7
  • 57
  • 64
  • With the Approach 2, I've noticed it easily leads to problems where I end up to the Approach 1 anyway, so I would recommend to go with Approach 1 in the first place. – toni_lehtimaki Oct 28 '19 at 10:18
  • 2
    @toni_lehtimaki why if it's basically the same thing? If you opt in the first to rebase on the current `package-lock.json` or accept "their" `package-lock.json` on the merge, they're actually the same. Or am I missing something here? – Guilherme Matuella Oct 30 '19 at 21:17
  • if I use absolute versions, I mean without `^` or `~`, is package-lock.json required? – JRichardsz Sep 03 '20 at 04:09
  • 3
    @JRichardsz, you still don't explicitly control transitive(indirect, 2nd+ level) dependencies – skyboyer Sep 03 '20 at 05:32
  • 3
    The last line in this answer is the key. After pulling and seeing that the merge conflict is only in the `package-lock.json` file(s), you can just do an `npm install` and it will resolve them for you (then you just have to stage/commit/etc.). TIL :) Thanks for that tip! – tklives Aug 27 '21 at 16:11
  • @skyboyer when should I run `yarn install` (Step 2)? Do I click "accept theirs" for example, in the merge dialog (using webstorm), and then run `yarn install`? – Giorgi Moniava Nov 10 '21 at 12:30
  • @giorgi moniava just `yarn`, not `yarn install`. Rest is correct, but I'm wondering why you are going to use "accept theirs" instead of leaving both changes? Are you fine to lose changes you've made so far? – skyboyer Nov 10 '21 at 12:33
  • @skyboyer What I did now web storm has "Apply All Non-Conflicting Changes" for both branches in merge dialog. Then there was one conflicting line which I resolved myself. Then did yarn is that what you meant? – Giorgi Moniava Nov 10 '21 at 13:29
  • @giorgimoniava yes – skyboyer Nov 10 '21 at 14:44
  • @skyboyer ok just one thing I don't get, above like I said I resolved the conflicts (inside `yarn.lock`): some using "Apply All Non-Conflicting Changes", one manually. Which conflicts did doing `yarn` afterwards resolve then? – Giorgi Moniava Nov 10 '21 at 14:48
  • 1
    wait, no need to resolve conflicts inside of `yarn.lock`. only inside of `package.json` – skyboyer Nov 10 '21 at 17:33
  • @skyboyer yeah when I was shown merge dialog I thought I also had to take action for `yarn.lock` file, seems I had to "ignore" it... – Giorgi Moniava Nov 10 '21 at 18:31
124

Yes it can have bad side effects, maybe not very often but for example you can have in package.json "moduleX": "^1.0.0" and you used to have "moduleX": "1.0.0" in package-lock.json.

By deleting package-lock.json and running npm install you could be updating to version 1.0.999 of moduleX without knowing about it and maybe they have created a bug or done a backwards breaking change (not following semantic versioning).

Anyway there is already a standard solution for it.

  1. Fix the conflict inside package.json
  2. Run: npm install --package-lock-only

Check out this link for more info:

https://docs.npmjs.com/cli/v6/configuring-npm/package-locks#resolving-lockfile-conflicts

sephoro
  • 150
  • 10
OZZIE
  • 6,609
  • 7
  • 55
  • 59
  • 2
    If "moduleX" needs to be 1.0.0 (not greater) then shouldn't 1.0.0 be specified in package.json instead of ^1.0.0 ? – SendETHToThisAddress Jun 01 '21 at 21:48
  • 1
    @SendETHToThisAddress if at the point of installing the module the latest is 1.0.0 how would you know that 1.0.99 will have breaking changes?? We can't predict the future. Also it might not have any breaking features or bugs - following semantic versioning just bugfixes, why would you opt out of them? In any case it should be a conscious descision when you update and not by misstake by following bad practices.. – OZZIE May 04 '22 at 13:48
  • 1
    This does not work for meg. No conflicts in package.json, but I still have more than 2000 changes and a bunch of conflixts in package-lock. json no matter how many times I run npm install --package-lock-only. –  May 24 '22 at 18:19
  • @Hfrav Meg perhaps you are rolling a really old node (and npm) version, latest is V18. – OZZIE May 25 '22 at 07:29
  • 1
    Nah. 14.16. The node version is controlled by the project, not me. –  May 25 '22 at 10:08
18

I know it's an old question but for future seekers, you can also use npm-merge-driver which try to automatically resolve the npm related files' merge issues.

Just install it globally npx npm-merge-driver install --global. You can read more about it here npm-merge-driver

Edit: Just want to warn people, who are interested in using above package, that sometime it can behave erratically and difficult to remove. So although it is a useful tool, it still need some work.

Edit: This repository is now archived and read only.

Taha
  • 434
  • 5
  • 12
-1

npm i --force does the work for me

shine odigie
  • 156
  • 1
  • 6