I know the main advantages of package-lock.json
and I agree with that. It not only locks the downloaded version in the last install, but also the uri... and that's required on most cases for being possible to replicate the most similar project as possible.
But one thing that seems weird to me is that package.json
has the feature of declaring a dependency like dependency: ^1.0.0
, that should make npm to download the most recent and compatible version of that package in each installation.
I'm working at a project that I actually need this. Otherwise every time my dependency releases a patch, it will be required to make a new commit updating package.json
only changing the version, so my pipeline can also overwrite package-lock.json
.
In short, it seems that while package.json
uses a feature... package-lock.json
prevents that one.
Am I missing something?
The point of package-lock.json
is to accurately represent the tree as it actually exists at a point in time, so that someone cloning the project gets exactly the same tree you had.
If you want to upgrade that dependency to a newer version, just use npm update
and then commit the updated package-lock.json
. Other members of your team will get that update as part of the normal process of picking up the latest.
More in the npmjs.com page on package locks.
Let's consider as scenario where you and I are on a team and our project uses nifty-lib
, with package.json
saying "nifty-lib": "^0.4.0"
, and we don't share package-lock.json
. Perhaps I've been working on the project a couple of months longer than you have and I got nifty-lib
v0.4.0 when I installed it. But when you picked it up and installed, you got v0.4.1 (a bugfix update which, sadly, introduced a new bug). At some point, you notice what seems like a bug in our project, but I can't replicate it. We spin in place for a while trying to figure out why it happens to you and not to me. In the end, we realize it's because it's actually a bug in nifty-lib
that they introduced in v0.4.1. Hopefully we then get 0.4.2 or something (or if there isn't one, we fix the bug and do a PR, meanwhile rolling back to 0.4.0 across the project).
If we'd been sharing package-lock.json
, we wouldn't have spun in place wondering why the problem happened to you and not to me, because you would have had the same version of nifty-lib
as me. As part of our normal cycle, we'd do npm update
periodically, and if a new bug showed up in our tests, we'd know from the commit history that it was because of a bug in a dependency.
Now, for "me" and "you" read "dev" and "production". :-)
Which is why package-lock.json
locks the version, but package.json
lets you say "this or better". package-lock.json
keeps your team unified on versions, but you can intentionally update with npm update
, which shows up in the commit history so you can track down regressions to it.