Semantic versioning (SemVer) is a rule for version numbers in the form MAJOR.MINOR.PATCH: increase MAJOR for changes that break compatibility, MINOR for new features that don't, and PATCH for bug fixes. Going from 2.4.1 to 3.0.0 tells users to expect breakage; going to 2.5.0 or 2.4.2 tells them upgrading should be safe.
The current specification is Semantic Versioning 2.0.0 at semver.org. npm's dependency ranges, covered below, are built on it.
The three numbers
| Part | Increase when | Reset | Example |
|---|---|---|---|
| MAJOR | Any backward-incompatible change to the public API | MINOR and PATCH to 0 | 1.4.2 → 2.0.0 |
| MINOR | New backward-compatible functionality, or a feature marked deprecated | PATCH to 0 | 1.4.2 → 1.5.0 |
| PATCH | Backward-compatible bug fixes only | Nothing | 1.4.2 → 1.4.3 |
Everything hinges on the public API. The spec's first rule is that software using SemVer "MUST declare a public API", in code or in documentation. "Breaking" means breaking that declared API: removing a function, renaming an option, changing a return type. Internal refactoring that callers can't see is a patch or a minor change, however large the diff.
The rules that trip people up
- Each number is a plain non-negative integer with no leading zeros.
1.02.3is invalid, and the parts compare numerically: 1.10.0 is newer than 1.9.0. - A released version never changes. Once 1.4.2 is published, its contents must not be modified. Fixing it means publishing 1.4.3.
- 0.y.z is the wild west. "Major version zero (0.y.z) is for initial development. Anything MAY change at any time." 1.0.0 is the version that defines the public API.
- Deprecation is a minor change. Marking a function as deprecated adds information without breaking anything; removing it later is the major change.
Pre-release versions and build metadata
A hyphen adds a pre-release label: 2.0.0-alpha, 2.0.0-beta.2, 2.0.0-rc.1. A pre-release is considered unstable and sorts before the release it leads to. A plus sign adds build metadata, such as 1.0.0+build.5, which is ignored for ordering.
Pre-release labels are compared piece by piece, and numeric pieces compare as numbers. That is why beta.11 sorts after beta.2. This is the real output of sorting with npm's semver library (version 7.5.4, bundled with npm 9.8.1):
1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0
And comparing 1.0.0+build.5 with 1.0.0+build.9 returns 0: equal, because build metadata does not count.
What ^ and ~ mean in package.json
npm dependencies are usually written as ranges, not exact versions. When you run npm install somepackage, npm saves the dependency with a caret by default, like "somepackage": "^1.2.3".
| Range | Means | Allows |
|---|---|---|
1.2.3 | Exactly this version | 1.2.3 only |
^1.2.3 | >=1.2.3 <2.0.0-0 | New minors and patches, no new major |
~1.2.3 | >=1.2.3 <1.3.0-0 | New patches only |
1.2.x or 1.2 | >=1.2.0 <1.3.0-0 | Any 1.2 patch |
^0.2.3 | >=0.2.3 <0.3.0-0 | Patches only, because 0.x minors may break |
^0.0.3 | >=0.0.3 <0.0.4-0 | Nothing newer |
* or "" | Any version | Everything (avoid) |
The caret rule in node-semver's words: it "allows changes that do not modify the left-most non-zero element". That single rule explains why ^1.2.3 is generous and ^0.0.3 is effectively pinned.
Here is the same test run against a list of candidate versions with npm's semver library:
^1.2.3 1.2.3 1.2.9 1.3.0 1.9.0
~1.2.3 1.2.3 1.2.9
1.2.x 1.2.3 1.2.9
^0.2.3 0.2.5
>=1.2.3 <2.0.0 1.2.3 1.2.9 1.3.0 1.9.0
The candidates were 1.2.3 1.2.9 1.3.0 1.9.0 2.0.0 2.0.0-beta.1 1.3.0-rc.1 0.2.5 0.3.0. Note that 1.3.0-rc.1 matched nothing, even though it falls inside ^1.2.3 numerically. By default, a pre-release only satisfies a range if some comparator in that range has the same MAJOR.MINOR.PATCH and is itself a pre-release. That stops a ^1.2.3 dependency from silently pulling in someone's release candidate.
You can reproduce this in any project that has the semver package installed:
node -e "const s=require('semver'); console.log(s.satisfies('1.9.0','^1.2.3'), s.satisfies('2.0.0','^1.2.3'))"
That prints true false.
Ranges and the lockfile
A range says which versions are acceptable; the lockfile records which one was actually installed. With a lockfile, everyone on the team gets the same versions until someone updates deliberately. See package-lock.json explained. When two packages need incompatible ranges of the same dependency, npm can't satisfy both; that is the situation behind npm ERESOLVE errors.
Bumping the version
npm can do the arithmetic and the Git tag for you:
npm version patch # 1.4.2 → 1.4.3
npm version minor # 1.4.2 → 1.5.0
npm version major # 1.4.2 → 2.0.0
npm version prerelease --preid=rc # 1.4.2 → 1.4.3-rc.0
In a Git repository, npm version also creates a version commit and a tag, and it refuses to run if the working directory has uncommitted changes. The results above match what the semver library's inc() function returns for 1.4.2.
What SemVer can't promise
A version number is a claim made by a person. A maintainer can misjudge what counts as breaking, or a "bug fix" can change behaviour that your code relied on. SemVer's FAQ covers the honest mistake: if a breaking change goes out in a minor version, fix it with a new minor release that restores compatibility, and don't modify the already-published version. Tests and a lockfile protect you from the cases where the number turns out to be wrong.
Frequently asked questions
- What does 0.x mean in semantic versioning?
- Major version zero is for initial development. The SemVer spec says anything may change at any time and the public API should not be considered stable. In practice many projects still treat a 0.MINOR bump as breaking, which is why npm's caret range ^0.2.3 only allows 0.2.x versions.
- Is v1.2.3 a valid semantic version?
- Strictly, no: the version is 1.2.3 and the v is a prefix that many projects use in Git tags. Tools vary. npm's semver library accepts v1.2.3 and normalises it to 1.2.3, but rejects 1.2 and 1.02.3.
- What should I do if I released a breaking change as a minor version?
- Don't edit or re-publish the released version, because SemVer forbids modifying a release. Publish a new minor version that restores compatibility, then release the breaking change properly as a new major version.
- When should a project release 1.0.0?
- When people depend on it. The SemVer FAQ says that if software is being used in production, or users depend on a stable API, it should probably already be 1.0.0.