I have a different opinion: don't track package-lock.json
at all.
The typical argument in favor of tracking it is this one taken from @skyboyer response:
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.
This is a falacious argument. Stability is not guaranteed by package-lock.json
. Stability is guaranteed by tests. If you cover your code with enough tests, a simple breaking change in the dependency graph that impacts your code will be detected at test-time.
Note two things:
- A breaking change in a dependency may totally not impact your product since you may not use the feature that was broken
- If a sub-dependency comes with a breaking change, your tests will detect it at then you can enforce the version of this sub-dependency in your
package.json
file.
Considering all the challenges and problems that tracking package-lock.json
brings, having to enforce a sub-dependency once every many years is worth it:
- conflicts on a daily basis
- not benefiting from the latest security fixes
- long-term maintenance nightmare that makes migrating to a major version very difficult: by locking sub-dependencies, major incompatiblities are not detected immediately and add up to the point where moving from a major framework version to the next becomes a nightmare of dependency hell - Angular guys, doesn't it seem familiar?
In the course of the last 5 years, we had to enforce a sub-dependency version once (yes, only once) and we have been working on hundreds of different projects. So definitely, things are much more stable today than they were 5 years ago and not tracking package-lock.json
is cheaper and easier than tracking it now.