الانتقال إلى المحتوى
DevOps والـ CloudCI/CD· 6 دقائق للقراءة

SemVer Is a Promise, Not a Guarantee: Version Ranges and Lockfiles

A caret range accepts releases nobody tested with your code. The lockfile is what makes an install repeatable.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

The build passed on Friday. On Monday, the same commit fails in CI with a test error nobody can reproduce on their laptop. Nobody touched 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

The belief: if a dependency follows semantic versioning, any upgrade within the same major version is safe. ^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.

The belief comes straight from the spec. says to increment the MINOR version "when you add functionality in a backward compatible manner" and the PATCH version "when you make backward compatible bug fixes." Package managers build on that promise: when we ran 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.

The half-truth: SemVer tells you what the author intends. It doesn't test what the new code does with yours.

What actually happens

The promise covers a declared public API

The spec starts from a contract: "Software using Semantic Versioning MUST declare a public API." Compatibility is measured against that API, so behavior outside it isn't covered: an undocumented default, the text of an error message you parse, timing, or the packages it depends on. Even a fix can break you. The spec defines a bug fix as "an internal change that fixes incorrect behavior", and if your code depended on that incorrect behavior, the patch release changes your results.

Nothing checks the promise

npm's docs recommend following the spec and say that if a change breaks dependents, "we strongly recommend incrementing the version major number." It's a recommendation, and the author picks the number. The spec's own FAQ asks "What do I do if I accidentally release a backward incompatible change as a minor version?" Its answer is to release a new minor that restores compatibility, because "it is unacceptable to modify versioned releases." The broken release stays published, and a range can still resolve to it. Before 1.0.0 the spec promises nothing at all: "Anything MAY change at any time." Dart's community shifts the meaning down one number for 0.x, but that's a convention, as the docs say.

A range is a list of versions you never tested

On the day we wrote this, ^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

A records the exact tree. npm's docs say 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

We reproduced the hook with Node 24.16 and npm 11.13. A developer ran 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:

Bash terminal
# 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

Without the lockfile, CI got Express 4.22.3 and 18 extra packages. Maybe 4.22.3 is fine; the problem is that your tests never ran against it before it shipped. With the lockfile, npm 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.

MythReality
Minor and patch releases can't break meThey promise not to break the declared public API, nothing else
Registries make sure packages follow SemVernpm recommends it; the spec's FAQ covers breaking minors released by accident
^4.17.1 means 4.17.1It means any 4.x from 4.17.1 up: 18 versions of Express in our run
0.x versions follow the same rules"Anything MAY change at any time" before 1.0.0
A lockfile keeps me on old versions foreverIt turns each update into a reviewed change that CI tests

How to apply it in your project

  1. Commit the lockfileMake sure package-lock.json is in source control and not in .gitignore. For a Flutter or Dart app, Dart's docs say the same about pubspec.lock: "you check your lockfile into your source control system!"
  2. Install from it in CI and in DockerUse npm ci in pipelines and Dockerfiles, as in the build cache article. If package.json and the lockfile don't match, npm ci exits with an error instead of quietly resolving new versions.
  3. Update on purposeSchedule 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.
  4. Be stricter with 0.x and critical packagesRead 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.

📚 المصادر

  1. Semantic Versioning 2.0.0
  2. About semantic versioning — npm Docs
  3. package-lock.json — npm Docs
  4. npm ci — npm Docs
  5. Package versioning — Dart

التعليقات (0)