Developer tools
semver versions: ^ and ~
An agreement on what each of the three version numbers means — so updates can be installed without fear.
Why it exists
A project has dozens of dependencies, and each one keeps shipping new versions. You can't check every update by hand, but you can't get stuck on old versions with security holes either. Semantic versioning (semver) is the author's promise: the version number itself tells you whether it will break your code. Then npm can safely install fixes and new features, but not breaking changes.
Where it's used
- dependencies in package.json:
"dayjs": "^1.11.13"is the allowed range of versions. - Releasing your own library:
npm version patch|minor|majorbumps the right number. - Update bots (Dependabot, Renovate) decide which update is safe from the version number.
- Release notes: "3.0.0 — breaking changes" warns you right away that you'll have to change code.
Syntax
MAJOR.MINOR.PATCH // 2.4.1
^1.2.0 // >=1.2.0 and <2.0.0
~1.2.0 // >=1.2.0 and <1.3.0
1.2.0 // exactly this versionExamples
const [major, minor, patch] = "1.4.2".split(".").map(Number);
major; // → 1
minor + patch; // → 6
"1.10.0" > "1.9.0"; // → false — strings compare character by characterfunction compareVersions(a, b) {
const x = a.split(".").map(Number);
const y = b.split(".").map(Number);
for (let i = 0; i < 3; i++) {
if (x[i] !== y[i]) return x[i] - y[i];
}
return 0;
}
compareVersions("1.10.0", "1.9.0") > 0; // → true
["1.10.0", "1.2.0", "1.9.0"].sort(compareVersions); // → ["1.2.0", "1.9.0", "1.10.0"]function caretAllows(base, version) {
const [bMajor] = base.split(".").map(Number);
const [vMajor] = version.split(".").map(Number);
return vMajor === bMajor && compare(version, base) >= 0;
}
function compare(a, b) {
const x = a.split(".").map(Number);
const y = b.split(".").map(Number);
return x[0] - y[0] || x[1] - y[1] || x[2] - y[2];
}
caretAllows("1.2.0", "1.9.4"); // → true
caretAllows("1.2.0", "2.0.0"); // → false — MAJOR changed
caretAllows("1.2.0", "1.1.9"); // → false — below the minimum^1.2.0 decides whether a version fits (for MAJOR ≥ 1)Results after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
The three numbers mean: When a number goes up, every number to its right resets to zero.
- PATCH (
1.4.2 → 1.4.3) — a bug was fixed, behavior didn't change; - MINOR (
1.4.2 → 1.5.0) — something was added, everything old still works; - MAJOR (
1.4.2 → 2.0.0) — incompatible changes: old code may break.
Ranges in package.json: ^ (the default for npm install) never changes the leftmost non-zero number. For ^1.2.0 that's MAJOR: 1.2.0…1.99.0 fit, 2.0.0 doesn't. 0.x versions count as unstable, so ^0.2.3 only allows 0.2.x, and ^0.0.3 allows exactly 0.0.3. The tilde ~1.2.3 is stricter: PATCH only, that is 1.2.x.
npm takes the list of versions from the registry, drops the ones that don't fit and installs the highest of the rest. Highest by numbers: 1.10.0 is newer than 1.9.0, even though as a string it's "smaller".
semver is an agreement, not a law: an author can accidentally break something in a MINOR release. That's why package-lock.json still pins exact versions.
Traps
How it differs from…
sort()andtoSorted()sort() with no argument compares elements as strings — wrong for versions. Pass a compare function that compares MAJOR, MINOR and PATCH as numbers.
When to pick something else
Don't write your own range parser for a real project: the full spec has pre-releases (2.0.0-beta.1), ranges with ||, >= and x. That's what the semver package is for — npm itself uses it. A mini version of your own is great for understanding how it all works.