SEMVER(7)
π SEMVER
okay bestie, welcome to semver(7) β the manual page for the semantic versioner for npm. yes, THE versioner. the one that decides whether your package.json is gonna install or absolutely lose its mind. let's get into it π
πΈ Usage
first up, install it like the icon it is:
$ npm install semver
then the JS API just... slays:
semver.valid('1.2.3') // '1.2.3'
semver.valid('a.b.c') // null
semver.clean(' =v1.2.3 ') // '1.2.3'
semver.satisfies('1.2.3', '1.x || >=2.5.0 || 5.0.0 - 7.2.3') // true
semver.gt('1.2.3', '9.8.7') // false
semver.lt('1.2.3', '9.8.7') // true
and as a command-line utility, which is where it really eats:
$ semver -h
Usage: semver <version> [<version> [...]] [-r <range> | -i <inc> | --preid <identifier> | -l | -rv]
Test if version(s) satisfy the supplied range(s), and sort them.
Multiple versions or ranges may be supplied, unless increment
option is specified. In that case, only a single version may
be used, and it is incremented by the specified level
Program exits successfully if any valid version satisfies
all supplied ranges, and prints all satisfying versions.
If no versions are valid, or ranges are not satisfied,
then exits failure.
Versions are printed in ascending order, so supplying
multiple versions to the utility will just sort them.
so real talk about what that means: multiple versions or ranges may be supplied, unless you specified the increment option β and in that case only a single version is allowed, and it gets incremented by the level you asked for. no multitasking on that one, it's a one-version-at-a-time situation π«Ά
the program exits successfully if any valid version satisfies all supplied ranges, and it prints all the satisfying versions. if no versions are valid, or ranges are not satisfied, then homegirl exits failure. and versions come out in ascending order, so if you hand the utility a bunch of versions it's just gonna sort them for you. king behavior honestly π
β¨ Versions
a "version" is described by the v2.0.0 specification found at http://semver.org/. that's the whole definition, no notes.
also: a leading = or v character is stripped off and ignored. so =v1.2.3 walks in and leaves as 1.2.3. the more you know π
π Ranges
okay so a version range is a set of comparators which specify versions that satisfy the range. think of it like the group chat where a version has to get along with everybody to be let in.
a comparator is composed of an operator and a version. the set of primitive operators is:
<β less than<=β less than or equal to>β greater than>=β greater than or equal to=β equal. and listen, if no operator is specified then equality is assumed, so this operator is optional, but it MAY be included. both are valid, do whatever feels right π
for example, the comparator >=1.2.7 would match the versions 1.2.7, 1.2.8, 2.5.3, and 1.3.9, but not the versions 1.2.6 or 1.1.0. no cap.
comparators can be joined by whitespace to form a comparator set, which is satisfied by the intersection of all of the comparators it includes. so everybody in the set has to say yes.
a range is composed of one or more comparator sets, joined by ||. a version matches a range if and only if every comparator in at least one of the ||-separated comparator sets is satisfied by the version. it's giving "any one of these friend groups will adopt you" energy π«Ά
for example, the range >=1.2.7 <1.3.0 would match the versions 1.2.7, 1.2.8, and 1.2.99, but not the versions 1.2.6, 1.3.0, or 1.1.0.
the range 1.2.7 || >=1.2.9 <2.0.0 would match the versions 1.2.7, 1.2.9, and 1.4.6, but not the versions 1.2.8 or 2.0.0. iconic.
π¦ Prerelease Tags
bestie this part is important so pay attention π if a version has a prerelease tag (for example, 1.2.3-alpha.3) then it will only be allowed to satisfy comparator sets if at least one comparator with the same [major, minor, patch] tuple also has a prerelease tag.
for example, the range >1.2.3-alpha.3 would be allowed to match the version 1.2.3-alpha.7, but it would not be satisfied by 3.4.5-alpha.9 β even though 3.4.5-alpha.9 is technically "greater than" 1.2.3-alpha.3 according to the SemVer sort rules. the version range only accepts prerelease tags on the 1.2.3 version. the version 3.4.5 would satisfy the range, because it does not have a prerelease flag, and 3.4.5 is greater than 1.2.3-alpha.7.
the purpose for this behavior is twofold:
- prerelease versions frequently are updated very quickly, and contain many breaking changes that are (by the author's design) not yet fit for public consumption. therefore, by default, they are excluded from range matching semantics. it's not you, alpha build, it's just that you're not ready for the public yet π
- a user who has opted into using a prerelease version has clearly indicated the intent to use that specific set of alpha/beta/rc versions. by including a prerelease tag in the range, the user is indicating that they are aware of the risk. however, it is still not appropriate to assume that they have opted into taking a similar risk on the next set of prerelease versions. consent matters, even for version numbers π«Ά
π Prerelease Identifiers
the method .inc takes an additional identifier string argument that will append the value of the string as a prerelease identifier:
> semver.inc('1.2.3', 'prerelease', 'beta')
'1.2.4-beta.0'
command-line example:
$ semver 1.2.3 -i prerelease --preid beta
1.2.4-beta.0
which then can be used to increment further:
$ semver 1.2.4-beta.0 -i prerelease
1.2.4-beta.1
look at her go π she's climbing
π Advanced Range Syntax
advanced range syntax desugars to primitive comparators in deterministic ways. it's giving "fancy on the outside, simple comparators on the inside" β¨
advanced ranges may be combined in the same way as primitive comparators β using white space or ||.
π Hyphen Ranges X.Y.Z - A.B.C
specifies an inclusive set.
1.2.3 - 2.3.4:=>=1.2.3 <=2.3.4
if a partial version is provided as the first version in the inclusive range, then the missing pieces are replaced with zeroes.
1.2 - 2.3.4:=>=1.2.0 <=2.3.4
if a partial version is provided as the second version in the inclusive range, then all versions that start with the supplied parts of the tuple are accepted, but nothing that would be greater than the provided tuple parts.
1.2.3 - 2.3:=>=1.2.3 <2.4.01.2.3 - 2:=>=1.2.3 <3.0.0
β X-Ranges 1.2.x 1.X 1.2.* *
any of X, x, or * may be used to "stand in" for one of the numeric values in the [major, minor, patch] tuple.
*:=>=0.0.0(any version satisfies β the people's champion π)1.x:=>=1.0.0 <2.0.0(matching major version)1.2.x:=>=1.2.0 <1.3.0(matching major and minor versions)
a partial version range is treated as an X-Range, so the special character is in fact optional.
""(empty string) :=*:=>=0.0.01:=1.x.x:=>=1.0.0 <2.0.01.2:=1.2.x:=>=1.2.0 <1.3.0
π₯ Tilde Ranges ~1.2.3 ~1.2 ~1
allows patch-level changes if a minor version is specified on the comparator. allows minor-level changes if not. she's flexible but she has boundaries π
~1.2.3:=>=1.2.3 <1.(2+1).0:=>=1.2.3 <1.3.0~1.2:=>=1.2.0 <1.(2+1).0:=>=1.2.0 <1.3.0(same as1.2.x)~1:=>=1.0.0 <(1+1).0.0:=>=1.0.0 <2.0.0(same as1.x)~0.2.3:=>=0.2.3 <0.(2+1).0:=>=0.2.3 <0.3.0~0.2:=>=0.2.0 <0.(2+1).0:=>=0.2.0 <0.3.0(same as0.2.x)~0:=>=0.0.0 <(0+1).0.0:=>=0.0.0 <1.0.0(same as0.x)~1.2.3-beta.2:=>=1.2.3-beta.2 <1.3.0β note that prereleases in the1.2.3version will be allowed, if they are greater than or equal tobeta.2. so,1.2.3-beta.4would be allowed, but1.2.4-beta.2would not, because it is a prerelease of a different[major, minor, patch]tuple. rules are rules π₯
π Caret Ranges ^1.2.3 ^0.2.5 ^0.0.4
allows changes that do not modify the left-most non-zero digit in the [major, minor, patch] tuple. in other words, this allows patch and minor updates for versions 1.0.0 and above, patch updates for versions 0.X >=0.1.0, and no updates for versions 0.0.X.
many authors treat a 0.x version as if the x were the major "breaking-change" indicator.
caret ranges are ideal when an author may make breaking changes between 0.2.4 and 0.3.0 releases, which is a common practice. however, it presumes that there will not be breaking changes between 0.2.4 and 0.2.5. it allows for changes that are presumed to be additive (but non-breaking), according to commonly observed practices. she's trusting you, don't make it weird π
^1.2.3:=>=1.2.3 <2.0.0^0.2.3:=>=0.2.3 <0.3.0^0.0.3:=>=0.0.3 <0.0.4^1.2.3-beta.2:=>=1.2.3-beta.2 <2.0.0β note that prereleases in the1.2.3version will be allowed, if they are greater than or equal tobeta.2. so,1.2.3-beta.4would be allowed, but1.2.4-beta.2would not, because it is a prerelease of a different[major, minor, patch]tuple.^0.0.3-beta:=>=0.0.3-beta <0.0.4β note that prereleases in the0.0.3version only will be allowed, if they are greater than or equal tobeta. so,0.0.3-pr.2would be allowed.
when parsing caret ranges, a missing patch value desugars to the number 0, but will allow flexibility within that value, even if the major and minor versions are both 0.
^1.2.x:=>=1.2.0 <2.0.0^0.0.x:=>=0.0.0 <0.1.0^0.0:=>=0.0.0 <0.1.0
a missing minor and patch values will desugar to zero, but also allow flexibility within those values, even if the major version is zero.
^1.x:=>=1.0.0 <2.0.0^0.x:=>=0.0.0 <1.0.0
π§ Range Grammar
putting all this together, here is a Backus-Naur grammar for ranges, for the benefit of parser authors (ily parser authors, this one's for you π«Ά):
range-set ::= range ( logical-or range ) *
logical-or ::= ( ' ' ) * '||' ( ' ' ) *
range ::= hyphen | simple ( ' ' simple ) * | ''
hyphen ::= partial ' - ' partial
simple ::= primitive | partial | tilde | caret
primitive ::= ( '<' | '>' | '>=' | '<=' | '=' | ) partial
partial ::= xr ( '.' xr ( '.' xr qualifier ? )? )?
xr ::= 'x' | 'X' | '*' | nr
nr ::= '0' | ['1'-'9']['0'-'9']+
tilde ::= '~' partial
caret ::= '^' partial
qualifier ::= ( '-' pre )? ( '+' build )?
pre ::= parts
build ::= parts
parts ::= part ( '.' part ) *
part ::= nr | [-0-9A-Za-z]+
and that's where this part wraps up β but don't go anywhere bestie, because there is more coming in part 2 πβ¨
π FUNCTIONS π
Okay bestie, here's the thing you gotta know before we get into the function list: every single method and class takes a final loose boolean argument. If it's true, it's all forgiving and chill about not-quite-valid semver strings π«Ά. The output it gives you back is always 100% strict though β no cap, that part never gets sloppy.
And Strict-mode Comparators and Ranges? They are strict about the SemVer strings they parse. Like, they will not be doing charity work for your malformed version strings. π
valid(v)β gives you back the parsed version, ornullif it's not valid π«inc(v, release)β gives you back the version incremented by the release type (major,premajor,minor,preminor,patch,prepatch, orprerelease), ornullif it's not validpremajorin one call bumps the version up to the next major version and then down to a prerelease of that major version β¨.preminorandprepatchwork exactly the same way, iconic behavior honestly.- If you call it from a non-prerelease version,
prereleaseworks the same asprepatch. It increments the patch version, then makes a prerelease. If the input version is already a prerelease, it simply increments it. Simple as that π
major(v)β gives you back the major version number πminor(v)β gives you back the minor version number πΈpatch(v)β gives you back the patch version number π©Ή
β¨ Comparison β¨
This is the part where we compare versions and it's giving very much "who's better" energy. Let's go:
gt(v1, v2)βv1 > v2gte(v1, v2)βv1 >= v2lt(v1, v2)βv1 < v2lte(v1, v2)βv1 <= v2eq(v1, v2)βv1 == v2. This istrueif they're logically equivalent, even if they're not the exact same string. You already know how to compare strings, bestie, don't make me say it πneq(v1, v2)βv1 != v2. The literal opposite ofeq, obviously πcmp(v1, comparator, v2)β you pass in a comparison string, and it calls the corresponding function from above."==="and"!=="do simple string comparison, but they're included for completeness. It throws if you hand it an invalid comparison string, so watch out πcompare(v1, v2)β returns0ifv1 == v2, or1ifv1is greater, or-1ifv2is greater. Sorts in ascending order if you pass it toArray.sort()πrcompare(v1, v2)β the reverse ofcompare. Sorts an array of versions in descending order when passed toArray.sort()πdiff(v1, v2)β returns the difference between two versions by the release type (major,premajor,minor,preminor,patch,prepatch, orprerelease), ornullif the versions are the same. Same version? Nothing to report. π―
π Ranges π
validRange(range)β gives you back the valid range, ornullif it's not validsatisfies(version, range)β returnstrueif the version satisfies the range βmaxSatisfying(versions, range)β returns the highest version in the list that satisfies the range, ornullif none of them do πgtr(version, range)β returnstrueif the version is greater than all the versions possible in the rangeltr(version, range)β returnstrueif the version is less than all the versions possible in the rangeoutside(version, range, hilo)β returnstrueif the version is outside the bounds of the range, in either the high or low direction. Thehiloargument must be either the string'>'or'<'. (Fun fact: this is the function thatgtrandltrcall under the hood π«Ά)
β οΈ okay this next bit is important, don't scroll past it!! Since ranges may be non-contiguous, a version might not be greater than a range, not less than a range, and not satisfy the range either! Like, all three at once. Wild, I know. π
For example, the range 1.2 <1.2.9 || >2.0.0 would have a hole from 1.2.9 until 2.0.0, so the version 1.2.10 would not be greater than the range (because 2.0.1 satisfies, which is higher), nor less than the range (since 1.2.8 satisfies, which is lower), and it also does not satisfy the range. Three-way rejection, it's giving "not me being excluded from every group chat" π
If you want to know if a version satisfies or does not satisfy a range, use the satisfies(version, range) function. That's literally what it's for, bestie, use the right tool π
And that's the whole page!! π¦ This edition of SEMVER(7) is dated March 2017 β vintage, honestly, but still slaying. Mwah, go parse some versions, you got this ππ
β¨
π¦ where this page comes from
- nixos 26.05: hueadm 1.2.1
- nixpkgs unstable: hueadm 1.2.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms