The guide
A Version Range Is a Set of Intervals, and Most of the Surprises Live at Their Edges
What a semantic version promises, how precedence is decided, why caret and tilde shrink below 1.0.0, and why prereleases and hidden -0 bounds keep catching people out.
- <2.0.0-0 the real upper end of ^1.2.3, below every 2.0.0 prerelease
- ^0.2.3 a caret that allows only 0.2.x, not 0.3.0
- 9007199254740991 the largest major, minor or patch the reference reads
- 1.0.0-alpha the lowest of the specification's eight precedence examples
Read the full guide Fold the guide
A version number looks like a label. Under Semantic Versioning it is closer to a promise, and a dependency range is a bet placed on that promise. Most of the trouble people have with ranges comes from reading them as words, when they are really sets.
What the three numbers promise
Semantic Versioning 2.0.0 asks a project to declare a public interface and then number its releases as major.minor.patch. A patch release may only fix behaviour that was wrong. A minor release may add to the interface without breaking anything that already works. A major release is where incompatible changes go, and when the major number rises the other two return to zero. Each number is a non-negative whole number written without leading zeros, so 1.9.0 is followed by 1.10.0, not by 2.0.0.
There is one large exception. A major version of zero is declared to be initial development, where anything may change at any time and the interface should not be treated as stable. That single sentence explains a lot of later behaviour.
Two optional tails hang off the end. A hyphen starts a prerelease, made of dot-separated identifiers drawn from ASCII letters, digits and the hyphen, none of them empty and none of the numeric ones with a leading zero. A plus sign starts build metadata under the same character rules. The difference between the two tails matters: a prerelease changes where a version sorts, while build metadata is ignored when deciding precedence, so 1.0.0+20130313144700 and 1.0.0+exp.sha.5114f85 rank exactly the same.
The specification talks about software, but the idea is older than any one tool. Versioning schemes of many kinds exist, and Semantic Versioning is one convention among them that attaches meaning to each position. Package managers lean on that meaning because they install other people’s code automatically, and a number that carries a promise is one of the few things a machine can reason about before it downloads anything.
How precedence is decided
Precedence is the order in which versions line up. The specification defines it by walking from the left and stopping at the first difference. Major, minor and patch are compared as numbers. If those are equal, a version with a prerelease comes before the same version without one, which is why 1.0.0-alpha sorts below 1.0.0.
When both versions have prereleases on the same three numbers, their identifiers are compared one at a time. Identifiers that are only digits are compared as numbers, identifiers with letters or hyphens are compared in ASCII order, and a numeric identifier always sorts below an alphanumeric one. If every identifier so far is equal, the shorter list comes first. This is a lexicographic order, the same idea as alphabetical order applied to a sequence of parts rather than a sequence of letters, and it produces the specification’s own example: 1.0.0-alpha, 1.0.0-alpha.1, 1.0.0-alpha.beta, 1.0.0-beta, 1.0.0-beta.2, 1.0.0-beta.11, 1.0.0-rc.1, 1.0.0.
Two details in that list surprise people. Beta.11 comes after beta.2 because 11 and 2 are compared as numbers, not as text. And alpha.1 comes before alpha.beta because a numeric identifier always loses to one with letters in it.
The result is a total order: any two versions can be compared, and the comparison never contradicts itself. That property is what makes everything else possible. Once versions sit on a single line, a range can be described the way a mathematician describes a stretch of that line.
A range is a set of intervals
The specification defines versions and their order. It does not define ranges. Each ecosystem writes its own range language, and this page follows exactly one: the grammar published with the node-semver package, the library the npm command-line tool uses to read ranges. Its README describes a range as one or more comparator sets joined by two vertical bars. A version matches the range when it satisfies every comparator in at least one of those sets.
Each comparator is an operator and a version, such as at least 1.2.7 or below 1.3.0. Every comparator set therefore carves out an interval on the line of versions, closed at an end written with an equals sign and open at an end written without one, like the half-open intervals of ordinary arithmetic. A range is a union of such intervals. Asking whether two ranges overlap, or whether one sits entirely inside another, becomes interval arithmetic rather than guesswork.
The shorthand forms are rewritten into plain comparators before anything is tested. The README gives the rules. An X-range or a partial version such as 1.2 or 1.2.x means at least 1.2.0 and below 1.3.0. A tilde allows patch-level changes when a minor version is given. A hyphen range such as 1.2.3 - 2.3 includes everything up to the end of the partial upper version, so it becomes at least 1.2.3 and below 2.4.0-0.
The caret is the one people use most and misread most. It allows changes that do not modify the left-most non-zero part of the version. For 1.2.3 that part is the major number, so ^1.2.3 accepts everything from 1.2.3 up to, but not including, 2.0.0. For 0.2.3 the left-most non-zero part is the minor number, so ^0.2.3 accepts only 0.2.x. For 0.0.3 it is the patch number, and ^0.0.3 accepts 0.0.3 alone. The narrowing is deliberate: it follows from the specification’s warning that anything may change while the major version is zero.
The edges where the surprises live
Look closely at how the reference package writes the upper end of ^1.2.3. It is not below 2.0.0 but below 2.0.0-0, printed as <2.0.0-0. The trailing -0 is the lowest possible prerelease of 2.0.0, so the bound sits below every 2.0.0 prerelease as well as below 2.0.0 itself. Without it, 2.0.0-beta.1 would sort inside the interval, because a prerelease sorts below its release.
A second rule closes the door from the other side. By default a prerelease version only satisfies a comparator set if some comparator in that set carries a prerelease on the same major, minor and patch numbers. The README explains the reasoning: someone who writes a prerelease into a range has opted in to that particular release train, not to the next one. So 1.2.3-beta.4 satisfies ^1.2.3-beta.2, while 1.2.4-beta.0 does not, even though it sorts inside the interval. A switch called include-prerelease turns the rule off.
Here is a worked example that the tool on this page loads first. Take the range ^1.2.3 || >=2.0.0-beta.2 <2.1.0 and the versions 1.2.2, 1.2.3, 1.4.0, 1.9.9-rc.1, 2.0.0-beta.0, 2.0.0-beta.3, 2.0.0, 2.0.1, 2.1.0 and 3.0.0. The first set becomes at least 1.2.3 and below 2.0.0-0. Five of the ten versions pass: 1.2.3, 1.4.0, 2.0.0-beta.3, 2.0.0 and 2.0.1. Version 1.2.2 fails the lower bound. Version 1.9.9-rc.1 sits inside the first interval but is a prerelease on a tuple no comparator names, so the prerelease rule rejects it. Version 2.0.0-beta.0 is on the right tuple but below beta.2. Versions 2.1.0 and 3.0.0 fall past both upper ends.
Edges also appear in the numbers themselves. JavaScript numbers hold whole numbers exactly only up to 9007199254740991, the value exposed as Number.MAX_SAFE_INTEGER, and the reference package refuses a major, minor or patch above it. Larger integers can be held exactly as BigInt values, and comparing prerelease identifiers that way keeps 1.0.0-9007199254740993 above 1.0.0-9007199254740992, which is what the specification’s numeric rule asks for. Even the reference package’s own helpers have edges: with prereleases included, its lowest-version helper answers 0.0.0 for a range that also accepts 0.0.0-0. Exact interval arithmetic is the safer basis, because a set either contains a version or it does not.
Checking a lockfile, and what no checker can see
A range is a promise about the future, and a lockfile is a record of the past. The npm documentation describes package-lock.json as a description of the exact tree that was installed, so that later installs can rebuild it. In lockfile versions 2 and 3 a packages map is keyed by install path: the empty key is the project itself, a key such as node_modules/alpha-lib is a top-level install, and deeper paths are nested copies. Version 1 lockfiles keep a dependencies map instead. Both files are JSON, so they can be read with any parser that follows the JSON standard.
Comparing the two is mechanical. For each range declared in package.json, find the version recorded at the top-level path and test it. A recorded version outside its declared range means the manifest and the lockfile have drifted apart, often because someone edited one by hand. The lockfile’s entry for the project itself also carries dependency fields copied from package.json, and when that copy disagrees with the manifest the lockfile is simply older than it.
Not every specifier is a range. Tags such as latest, aliases that install one package under another name, git URLs, local file paths, tarball URLs and workspace references all appear in real manifests. None of them can be tested against a version number, and the honest move is to list them as not checked rather than to guess.
The limits are worth stating plainly. A range check knows nothing about the registry, so it cannot tell you whether a newer version exists or whether a release was withdrawn. It cannot tell you whether a project kept its promise, because a patch release that breaks your code still has a patch number. And it covers one grammar: other ecosystems write ranges with different operators and different defaults, and dependency hell, the frustration of incompatible shared requirements, has a long history across all of them. What exact range arithmetic can do is narrower and still useful: it says precisely which versions a range admits, why each rejected one fails, and where two ranges meet.
Sources
Every source consulted for this guide. Links checked October 30, 2026.
- Semantic Versioning 2.0.0 Semantic Versioning (semver.org, CC BY 3.0)
- npm/node-semver: The semver parser for node (the one npm uses) npm, node-semver repository (ISC licence)
- node-semver/range.bnf npm, node-semver repository (ISC licence)
- package-lock.json npm, npm/cli repository documentation (Artistic License 2.0)
- Software versioning Wikipedia (CC BY-SA)
- Interval (mathematics) Wikipedia (CC BY-SA)
- Total order Wikipedia (CC BY-SA)
- Lexicographic order Wikipedia (CC BY-SA)
- Dependency hell Wikipedia (CC BY-SA)
- Package manager Wikipedia (CC BY-SA)
- RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format RFC Editor (IETF)
- BigInt - JavaScript MDN Web Docs (Mozilla, CC BY-SA)
- Number.MAX_SAFE_INTEGER - JavaScript MDN Web Docs (Mozilla, CC BY-SA)