218

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?

2
  • Refer this answer - stackoverflow.com/a/44297998/2251411 Jan 10, 2019 at 8: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 7, 2020 at 8:37

4 Answers 4

192

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.

12
  • 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. Oct 28, 2019 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? Oct 30, 2019 at 21:17
  • 3
    @JRichardsz, you still don't explicitly control transitive(indirect, 2nd+ level) dependencies
    – skyboyer
    Sep 3, 2020 at 5: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, 2021 at 16:11
  • 1
    wait, no need to resolve conflicts inside of yarn.lock. only inside of package.json
    – skyboyer
    Nov 10, 2021 at 17:33
126

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

5
  • 3
    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 ? Jun 1, 2021 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 4, 2022 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.
    – user9347168
    May 24, 2022 at 18:19
  • @Hfrav Meg perhaps you are rolling a really old node (and npm) version, latest is V18.
    – OZZIE
    May 25, 2022 at 7:29
  • 1
    Nah. 14.16. The node version is controlled by the project, not me.
    – user9347168
    May 25, 2022 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.

0

npm i --force does the work for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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