Practice Session: Reading npm Semver Ranges
Every dependency line in a package.json is a small promise about which future releases you will accept. Getting the range syntax wrong either freezes you on old versions or lets a surprise in. This unit is designed to take about forty minutes: read the explanation, do the exercises without looking, then check yourself against the answers at the bottom.
Objectives
- Translate caret, tilde and x-ranges into explicit lower and upper bounds.
- Explain why ranges behave differently below 1.0.0.
- Predict whether a pre-release version will be selected.
- Choose a sensible range for a library's own dependencies and peer dependencies.
Explanation
Caret (^)
A caret allows changes that do not modify the left-most non-zero digit. For most packages that means "same major":
^1.2.3means>=1.2.3 <2.0.0^0.2.3means>=0.2.3 <0.3.0^0.0.3means>=0.0.3 <0.0.4
That sliding behaviour is why the 0.x minor digit effectively acts as a breaking-change signal in the npm ecosystem.
Tilde (~)
A tilde allows patch-level changes when a minor version is given: ~1.2.3 means >=1.2.3 <1.3.0. With only a major, ~1 means >=1.0.0 <2.0.0.
X-ranges and hyphens
1.x, 1.* and plain 1 all mean "any 1.y.z". A hyphen range such as 1.2.3 - 2.3.4 is inclusive on both ends.
Pre-releases
The node-semver rules used by npm exclude pre-release versions from a range unless the comparator itself names a pre-release on the same major.minor.patch. So ^1.2.3-beta.2 will accept 1.2.3-beta.4 but not 1.2.4-beta.1. A plain ^1.2.3 never picks up any beta. This protects users from accidentally installing unstable builds.
Practice exercises
- Write the explicit bounds for
~0.4.1. - Does
^2.0.0match3.0.0-rc.1? - Does
^2.1.0-alpha.1match2.1.0-alpha.7? And2.1.5? - Your library uses a helper at 0.9.2 and wants bug fixes automatically. Which range accepts 0.9.x but not 0.10.0?
- Your plugin works with Host versions 3 and 4. Write a peer dependency range.
- Rewrite
>=1.0.0 <2.0.0as a caret range. - Why is
"*"a poor choice for a production dependency of a library?
Applying it to your own package
For runtime dependencies of a library, a caret range on the lowest version you have actually tested is the common default. It lets your users' package managers deduplicate shared dependencies. Pinning exact versions in a library tends to cause duplicate copies in your consumers' trees. Peer dependency ranges deserve extra care, because they constrain the host application's choices: widen them only after testing against each major you list. Applications are a different story; there, your lockfile does the pinning.
You can check any range on the command line with the semver package's CLI, for example npx semver -r "^0.4.1" 0.4.9 0.5.0, which prints only the versions that satisfy the range.
Self-check answers
>=0.4.1 <0.5.0.- No. It is a different major, and it is a pre-release on a different version tuple.
- Yes to
2.1.0-alpha.7, because it is a pre-release of the same 2.1.0 tuple and sorts higher. Yes to2.1.5, which is a normal release inside the range. ^0.9.2or~0.9.2; both give>=0.9.2 <0.10.0."^3.0.0 || ^4.0.0".^1.0.0.- It accepts any future major, including breaking ones, and gives the resolver no guidance.
If you missed questions 2 or 3, reread the pre-release paragraph; it is the part most people misremember. The semver edge cases FAQ covers related questions.