8

I know there are numerous issues about this, and I discovered the command npm ci that is supposed to not change package-lock.json, but when I run npm ci it fails:

ERR! cipm can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with `npm install` before continuing.

Also tried another solution involving deleting my node_modules directory and running npm i again, but that's not changing the outcome.

I'm a junior dev working with a team remotely.. I was given a task, so I created a new branch on Gitlab, pulled it down to my local machine and ran npm i to get up-to-speed...

But it keeps changing my package-lock.json DRAMATICALLY(it adds like 20,000 lines of code)

Committing that to the team's project seems insane to me. Anyone have advice?

2
  • it means they don't agree. Sep 4, 2020 at 13:55
  • 2
    i'd talk with your team to see exactly what happened and what their process is. Sep 4, 2020 at 13:58

2 Answers 2

11

Update: OP wound up needing to make use of yarn which their team and project made use of. If anyone finds a yarn.lock in their project root, this is an indication that yarn is involved and any package-lock.json, if there is one, is possibly outdated.


TL;DR: it sounds like the package-lock.json needs some updates and resolution, which is done primarily with npm install.

It sounds like the package-lock.json no longer "agrees" with your package.json. It also sounds like others on your team are avoiding committing the changes to your package-lock.json; this is a bad practice in my experience as it only deepens any divergence in their contents. Over time they can be out of sync when dependencies of dependencies may publish a bug fix release and potentially un-publish a previous version.

As a junior dev on this team, I would bring this up to your development/team lead and ask their preferred approach here. If there was a major dependency intentionally removed and that has a lot of its own dependencies, it could cause a large removal of lines from package-lock.json and look severe to one less accustomed to it.

Extra context:

During an install, npm installs the dependencies and development dependencies listed in your package.json. In the process it's possible and increasingly likely over time, that some of the dependencies of those dependencies, which are needed to execute, will overlap and often with conflicting versions. The execution of npm i(nstall) will attempt to reconcile all these competing versions of sub-dependencies for you.

In a Continuous Integration context, it is desirable for the alternate command npm ci to be used, which explicitly installs only what's resolved already, from package-lock.json (the formerly known as "shrinkwrap"). This is meant to reduce the "but it works on my machine!" moments. As a developer, I've found it to be preferable to use npm install, as this alerts the developers more quickly to any dependency resolution issues and keeps the package-lock.json up to date.

2
  • 1
    This took me a minute to wrap my head around, but thank you for the advice. I sorted it out with my tech lead. Thank you for the well-thought out and descriptive answer!
    – Cin88
    Sep 4, 2020 at 15:14
  • Cheers @Cin88. It's certainly an odd thing to start off with, but I'm glad to hear you've gotten things sorted out. Sep 4, 2020 at 16:25
3

Since this post got so many views I thought I'd come back and post what I found.

Yarn and NPM both update and install packages and dependencies, but the difference is:

yarn creates a file called yarn.lock

npm install creates a file called package-lock.json.

I didn't know this at the time, so when I cloned the project repo to my local machine, I ran npm i which created the package-lock.json. My teammates were already using yarn, however.

So make sure you use the one already being used.

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.