Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm install changes my package-lock.json, will that interfere with the remote code?

Tags:

javascript

npm

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?

like image 636
Cin88 Avatar asked Sep 04 '20 13:09

Cin88


People also ask

Does npm install change package-lock json?

npm install will generate a new package-lock. json if it does not exist or it will update the dependency tree if it does not match the packages specified in the package. json . npm ci will install packages based on package-lock.

Should I push changes to package-lock json?

While working on a shared project it is highly recommended to commit the package-lock file to source control: this will allow anyone else on your team, your deployments, your continuous integration, and anyone else who runs npm install in your package source to get the exact same dependency tree that you were ...

What causes package-lock json to change?

The reason package-lock. json may change automatically when you run npm install is because NPM is updating the package-lock. json file to accurately reflect all the dependencies it has downloaded since it may have gotten more up-to-date versions of some of them. Once NPM updates the package-lock.

Is it safe to remove package-lock json?

json that result in two different installs. You may have noticed it before; you install a package using npm and suddenly a new file called package-lock. json appears in your project directory. Don't delete that package-lock file, run npm install and regenerate it!


2 Answers

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.

like image 73
Eric McCormick Avatar answered Oct 14 '22 05:10

Eric McCormick


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.

like image 42
Cin88 Avatar answered Oct 14 '22 07:10

Cin88