
SemVer Is a Promise, Not a Guarantee: Version Ranges and Lockfiles
- كاتب موثّق
محتويات المقال
package.json, and every dependency in it follows semantic versioning. The difference is inside node_modules: the laptops installed months ago, the CI runner installed this morning, and both installs satisfy the same ^ ranges.
The common misconception
^4.17.1 means "4.17.1 or anything compatible with it", so whichever version you get is as good as the one you tested.
npm install express@4.17.1, npm wrote ^4.17.1 into package.json. Dart's docs spell out the reasoning: "If you trust them, then if you know your package works with 2.3.5 of theirs, you can rely on it working all the way up to 3.0.0." And most of the time, it does work.
What actually happens
The promise covers a declared public API
Nothing checks the promise
A range is a list of versions you never tested
^4.17.1 matched 18 published versions of Express, from 4.17.1 to 4.22.3. Which one you get depends on when you install. The ranges inside your dependencies move too: in our run, the same package.json installed 68 packages without a lockfile and 50 with one. The spec has a name for trusting too many future versions: "version promiscuity (assuming compatibility with more future versions than is reasonable)."
The lockfile records what you tested
package-lock.json "describes the exact tree that was generated, such that subsequent installs are able to generate identical trees, regardless of intermediate dependency updates": the version, the source URL and an integrity hash for every package, including the transitive ones. For CI there's npm ci, which "will never write to package.json or any of the package-locks: installs are essentially frozen." Dart's pubspec.lock works the same way: "Once your app is locked, it stays locked until you manually tell it to update the lockfile."
The fix: commit the lockfile and install from it
npm install express@4.17.1, which saved "express": "^4.17.1" in package.json and pinned 4.17.1 in the lockfile. Then CI installed the same project twice:
# package.json: "express": "^4.17.1"; package-lock.json is ignorednpm view express@"^4.17.1" version | wc -l# 18npm install# added 68 packages, and audited 69 packages in 6snpm ls express# `-- express@4.22.3# package-lock.json is committed with the codegrep -A2 '"node_modules/express"' package-lock.json# "node_modules/express": {# "version": "4.17.1",# "resolved": "https://registry.npmjs.org/express/-/express-4.17.1.tgz",npm ci# added 50 packages, and audited 51 packages in 2snpm ls express# `-- express@4.17.1npm outdated# Package Current Wanted Latest Location Depended by# express 4.17.1 4.22.3 5.3.0 node_modules/express shop-apinpm ci installed exactly what the developer had. npm outdated then shows the three numbers that matter: Current is what the lockfile pins, Wanted is the newest version the range allows, and Latest is a new major that the range will never pick up on its own.
^4.17.1 means 4.17.1 | |
How to apply it in your project
Commit the lockfile Make sure package-lock.jsonis in source control and not in.gitignore. For a Flutter or Dart app, Dart's docs say the same aboutpubspec.lock: "you check your lockfile into your source control system!"Install from it in CI and in Docker Use npm ciin pipelines and Dockerfiles, as in the build cache article. Ifpackage.jsonand the lockfile don't match,npm ciexits with an error instead of quietly resolving new versions.Update on purpose Schedule dependency updates as their own pull requests, so the lockfile diff and the test results arrive together. When something breaks, you know which update did it. Be stricter with 0.x and critical packages Read the changelog before you accept a 0.x release, or an update to a package that handles payments or security, even when the version number says it's minor.
Key takeaways
SemVer is a promise about a declared public API, made by the author and checked by nobody. A caret range accepts future releases your tests never saw, and transitive ranges move as well. The lockfile records the exact tree you tested; commit it and install with npm ci.Lockfiles freeze vulnerabilities too, so update regularly through reviewed, tested pull requests. Before 1.0.0, treat every release as potentially breaking.
التعليقات (0)