Semver & Version Ranges
A dependency in package.json is rarely an exact version. "lodash": "^4.17.21" is a range: a promise that any version in it will work. The package manager picks the highest version in the range that the registry has, and the lockfile freezes that choice. To read and write ranges correctly you need Semantic Versioning and npm's range syntax.
MAJOR.MINOR.PATCH
| Part | Bump it when | Example |
|---|---|---|
| MAJOR | you break the public API | 4.17.21 → 5.0.0 |
| MINOR | you add features, backwards compatible | 4.17.21 → 4.18.0 |
| PATCH | you fix bugs, backwards compatible | 4.17.21 → 4.17.22 |
| pre-release | a version before the release: -alpha.1, -rc.0 |
5.0.0-rc.0 < 5.0.0 |
| build metadata | +build.7: ignored for precedence |
1.0.0+build.7 = 1.0.0 |
Below 1.0.0 the rules shift one place to the right: in 0.28.2, the minor number is the breaking one. That's why ^0.28.2 does not accept 0.29.0.
Range syntax
| Range | Means | Matches |
|---|---|---|
4.17.21 |
exactly | 4.17.21 |
^4.17.21 |
same major (npm's default for npm install) |
≥4.17.21 <5.0.0 |
^0.28.2 |
same minor, because major is 0 | ≥0.28.2 <0.29.0 |
~4.17.21 |
same minor | ≥4.17.21 <4.18.0 |
4.x, 4 |
any 4.*.* | ≥4.0.0 <5.0.0 |
>=18 <21 |
comparator set | 18.0.0 to 20.x |
^18 || ^19 |
either range | React 18 or 19 |
1.2.3 - 2.3.4 |
hyphen range, inclusive | ≥1.2.3 ≤2.3.4 |
*, "" |
anything | any non-prerelease |
Checking ranges with the real semver package
semver is the package npm itself uses for this. Its CLI prints the versions that satisfy a range, sorted, and exits with status 1 if none do:
mkdir -p sv && cd sv
npm init -y > /dev/null && npm install semver@7.8.5 > /dev/null
npx semver -r '^4.17.0' 4.16.6 4.17.21 4.18.1 5.0.0
echo ---
npx semver -r '~4.17.0' 4.16.6 4.17.21 4.18.1 5.0.0
echo ---
npx semver -r '^0.28.2' 0.28.1 0.28.9 0.29.0 1.0.0
echo ---
npx semver -r '^5.0.0' 5.0.0-rc.1 5.0.0 5.1.0
echo ---
npx semver -r '^5.0.0-rc.0' 5.0.0-rc.1 5.0.0 5.1.0
echo ---
npx semver -r '^6' 4.18.1 || echo "no match, exit $?"
npx semver -i minor 4.17.21
npx semver -i premajor --preid rc 4.17.21
npx semver 1.10.0 1.9.0 1.2.10 1.2.94.17.21
4.18.1
---
4.17.21
---
0.28.9
---
5.0.0
5.1.0
---
5.0.0-rc.1
5.0.0
5.1.0
---
no match, exit 1
4.18.0
5.0.0-rc.0
1.2.9
1.2.10
1.9.0
1.10.0
Two things trip people up:
- Sorting is numeric per part:
1.10.0>1.9.0, so plain string sorting is wrong. - Pre-releases are opt-in:
^5.0.0doesn't match5.0.0-rc.1. A range only accepts pre-releases of the sameMAJOR.MINOR.PATCHthat it names itself, so^5.0.0-rc.0matchesrc.1.
The same library from JavaScript, as a build script would use it:
mkdir -p sv && cd sv
npm init -y > /dev/null && npm install semver@7.8.5 > /dev/null
cat > check.js <<'JS'
const semver = require('semver');
const available = ['4.17.21', '4.18.1'];
for (const range of ['^4.17.0', '~4.17.0', '4.17.21', '>=5']) {
console.log(range.padEnd(9), '->', semver.maxSatisfying(available, range));
}
console.log(semver.validRange('^4.17.0'));
console.log(semver.satisfies(process.versions.node, '>=20'), semver.major(process.versions.node));
console.log(semver.coerce('v24.22 LTS').version);
JS
node check.js^4.17.0 -> 4.18.1
~4.17.0 -> 4.17.21
4.17.21 -> 4.17.21
>=5 -> null
>=4.17.0 <5.0.0-0
true 24
24.22.0
maxSatisfying is the heart of every resolver: from the versions the registry has, pick the highest one in the range. validRange shows what a caret really means as comparators: >=4.17.0 <5.0.0-0. The -0 is the lowest possible pre-release of 5.0.0, so the upper bound also shuts out 5.0.0-rc.1. coerce rescues a sloppy version string (here it dropped the v, filled in the patch and ignored the rest).
A caret matcher from scratch
const parse = (v) => v.split('.').map(Number);
const cmp = (a, b) => {
for (let i = 0; i < 3; i++) if (a[i] !== b[i]) return a[i] - b[i];
return 0;
};
// ^X.Y.Z: >= X.Y.Z and below the next bump of the first non-zero part.
function caretUpper([maj, min, pat]) {
if (maj > 0) return [maj + 1, 0, 0];
if (min > 0) return [0, min + 1, 0];
return [0, 0, pat + 1];
}
function satisfiesCaret(version, range) {
const lo = parse(range.slice(1)), v = parse(version);
return cmp(v, lo) >= 0 && cmp(v, caretUpper(lo)) < 0;
}
const versions = ['0.0.3', '0.0.4', '0.28.1', '0.28.9', '0.29.0', '4.17.21', '4.18.1', '5.0.0'];
for (const range of ['^4.17.0', '^0.28.2', '^0.0.3']) {
const ok = versions.filter((v) => satisfiesCaret(v, range));
const best = ok.sort((a, b) => cmp(parse(b), parse(a)))[0];
console.log(`${range.padEnd(8)} matches ${ok.join(', ').padEnd(16)} -> picks ${best}`);
}^4.17.0 matches 4.18.1, 4.17.21 -> picks 4.18.1
^0.28.2 matches 0.28.9 -> picks 0.28.9
^0.0.3 matches 0.0.3 -> picks 0.0.3
^0.0.3 matches only 0.0.3: for 0.0.x packages every release may break.
Which range should you write?
| Situation | Write |
|---|---|
| a normal app dependency | ^x.y.z (the default) and commit the lockfile |
| a library's dependency | ^x.y.z, so consumers can share one copy |
| a tool that changes output in minors (TypeScript, a formatter) | ~x.y.z or exact |
| a peer dependency | the widest range you test: ^18 || ^19 |
| a 0.x dependency | ^0.y.z already pins the minor; read its changelog anyway |
npm install --save-exact (or save-exact=true in .npmrc) writes exact versions. Exact versions in package.json don't make the transitive tree exact, though; only the lockfile does.
Test yourself
Which versions does ^0.28.2 accept?
- >=0.28.2 <1.0.0
- >=0.28.2 <0.29.0
- only 0.28.2
- >=0.28.2 <0.28.3
Does ^5.0.0 match 5.0.0-rc.1?
- Yes, rc.1 is 5.0.0
- No, pre-releases are excluded unless the range itself names a pre-release of 5.0.0
- Only with --force
- Yes, but only for peer dependencies
What does semver.maxSatisfying(['4.17.21','4.18.1'], '~4.17.0') return?
- 4.18.1
- 4.17.21
- null
- 4.17.0