1001Ferramentas
📦 Dev

SemVer Range Tester

Tests which versions satisfy a semver range (^1.2.3, ~2.0, >=1 <2) and explains the range as a lower and an upper bound.

Accepts ^1.2.3, ~2.0, >=1 <2, 1.2.x, 1.2.3 - 2.3.4 and ^1 || ^2.

Caret, tilde and prereleases

The caret keeps the leftmost non-zero field fixed, so ^1.2.3 allows up to 1.999.999 while ^0.2.3 stops at 0.2.x, because in the 0.x line the minor field carries the breaking changes. The tilde is stricter: ~1.2.3 only moves the patch. Writing ~1 is the exception, since with no minor there is nothing below to hold, and it behaves like ^1.

A prerelease such as 2.0.0-rc.1 sorts below 2.0.0, so it fits inside >=1.0.0 <2.0.0 by pure comparison. npm still refuses it unless the range itself names a prerelease with the same major.minor.patch, which is why ^1.2.3 never picks up 1.5.0-beta but >=1.5.0-beta.0 does.

What the range ^1.2.3 actually accepts

The package.json asks for ^1.2.3, the lockfile resolved 1.9.5, and someone in code review wants to know whether that is correct. Paste the range and the list of published versions: every line comes back with a tick or a cross, the comparator set that matched and the exact reason for the refusal, such as 2.0.0 above the upper bound <2.0.0. Nothing leaves the browser.

Before any comparison happens, each operator becomes a pair of bounds. The caret ^1.2.3 opens up to but not including 2.0.0, while ^0.2.3 stops at 0.3.0, because on a 0.x line it is the minor field that carries breaking changes. The tilde ~1.2.3 only frees the patch. Wildcards like 1.2.x, hyphen ranges like 1.2.3 - 2.3.4 and double-bar alternatives are normalized too, so two ranges written differently can be compared.

The trap is the prerelease. By plain ordering, 2.0.0-rc.1 sorts below 2.0.0 and would fit inside ^1.2.3, yet npm only accepts a hyphenated version when the range itself names a prerelease with the same major.minor.patch. That is why ^1.2.3 never installs 1.5.0-beta while >=1.5.0-beta.0 <2.0.0 installs it without complaining. The include-prereleases box shows both scenarios side by side.

Frequently asked questions

What is the practical difference between ^1.2.3 and ~1.2.3?
The caret pins the leftmost non-zero field and frees everything below it, so ^1.2.3 accepts from 1.2.3 up to 1.999.999. The tilde pins one field more, so ~1.2.3 spans only 1.2.3 to 1.2.999. In production the tilde brings fixes; the caret also brings new features.
Why is the beta version reported as refused?
By default the tool follows the npm rule: a hyphenated version only qualifies if the range mentions a prerelease with the same major.minor.patch tuple. Tick the include-prereleases box to see the result of the plain numeric comparison, which is what some other ecosystems use.
What do 1.2.x and ^0.0.3 mean?
The wildcard 1.2.x expands to >=1.2.0 <1.3.0, meaning any patch of that minor. And ^0.0.3 is the tightest caret case: it expands to >=0.0.3 <0.0.4 and accepts exactly one version, because on a 0.0.x line the spec assumes any bump may break consumers.

Related Tools