1SEMVER(7) SEMVER(7)
2
3
4
6 semver - The semantic versioner for npm
7
9 npm install --save semver
10 `
11
13 As a node module:
14
15 const semver = require('semver')
16
17 semver.valid('1.2.3') // '1.2.3'
18 semver.valid('a.b.c') // null
19 semver.clean(' =v1.2.3 ') // '1.2.3'
20 semver.satisfies('1.2.3', '1.x || >=2.5.0 || 5.0.0 - 7.2.3') // true
21 semver.gt('1.2.3', '9.8.7') // false
22 semver.lt('1.2.3', '9.8.7') // true
23 semver.valid(semver.coerce('v2')) // '2.0.0'
24 semver.valid(semver.coerce('42.6.7.9.3-alpha')) // '42.6.7'
25
26 As a command-line utility:
27
28 $ semver -h
29
30 A JavaScript implementation of the http://semver.org/ specification
31 Copyright Isaac Z. Schlueter
32
33 Usage: semver [options] <version> [<version> [...]]
34 Prints valid versions sorted by SemVer precedence
35
36 Options:
37 -r --range <range>
38 Print versions that match the specified range.
39
40 -i --increment [<level>]
41 Increment a version by the specified level. Level can
42 be one of: major, minor, patch, premajor, preminor,
43 prepatch, or prerelease. Default level is 'patch'.
44 Only one version may be specified.
45
46 --preid <identifier>
47 Identifier to be used to prefix premajor, preminor,
48 prepatch or prerelease version increments.
49
50 -l --loose
51 Interpret versions and ranges loosely
52
53 -p --include-prerelease
54 Always include prerelease versions in range matching
55
56 -c --coerce
57 Coerce a string into SemVer if possible
58 (does not imply --loose)
59
60 Program exits successfully if any valid version satisfies
61 all supplied ranges, and prints all satisfying versions.
62
63 If no satisfying versions are found, then exits failure.
64
65 Versions are printed in ascending order, so supplying
66 multiple versions to the utility will just sort them.
67
69 A "version" is described by the v2.0.0 specification found at
70 http://semver.org/.
71
72 A leading "=" or "v" character is stripped off and ignored.
73
75 A version range is a set of comparators which specify versions that
76 satisfy the range.
77
78 A comparator is composed of an operator and a version. The set of
79 primitive operators is:
80
81 · < Less than
82
83 · <= Less than or equal to
84
85 · > Greater than
86
87 · >= Greater than or equal to
88
89 · = Equal. If no operator is specified, then equality is assumed, so
90 this operator is optional, but MAY be included.
91
92
93 For example, the comparator >=1.2.7 would match the versions 1.2.7,
94 1.2.8, 2.5.3, and 1.3.9, but not the versions 1.2.6 or 1.1.0.
95
96 Comparators can be joined by whitespace to form a comparator set, which
97 is satisfied by the intersection of all of the comparators it includes.
98
99 A range is composed of one or more comparator sets, joined by ||. A
100 version matches a range if and only if every comparator in at least one
101 of the ||-separated comparator sets is satisfied by the version.
102
103 For example, the range >=1.2.7 <1.3.0 would match the versions 1.2.7,
104 1.2.8, and 1.2.99, but not the versions 1.2.6, 1.3.0, or 1.1.0.
105
106 The range 1.2.7 || >=1.2.9 <2.0.0 would match the versions 1.2.7,
107 1.2.9, and 1.4.6, but not the versions 1.2.8 or 2.0.0.
108
109 Prerelease Tags
110 If a version has a prerelease tag (for example, 1.2.3-alpha.3) then it
111 will only be allowed to satisfy comparator sets if at least one com‐
112 parator with the same [major, minor, patch] tuple also has a prerelease
113 tag.
114
115 For example, the range >1.2.3-alpha.3 would be allowed to match the
116 version 1.2.3-alpha.7, but it would not be satisfied by 3.4.5-alpha.9,
117 even though 3.4.5-alpha.9 is technically "greater than" 1.2.3-alpha.3
118 according to the SemVer sort rules. The version range only accepts
119 prerelease tags on the 1.2.3 version. The version 3.4.5 would satisfy
120 the range, because it does not have a prerelease flag, and 3.4.5 is
121 greater than 1.2.3-alpha.7.
122
123 The purpose for this behavior is twofold. First, prerelease versions
124 frequently are updated very quickly, and contain many breaking changes
125 that are (by the author's design) not yet fit for public consumption.
126 Therefore, by default, they are excluded from range matching semantics.
127
128 Second, a user who has opted into using a prerelease version has
129 clearly indicated the intent to use that specific set of alpha/beta/rc
130 versions. By including a prerelease tag in the range, the user is
131 indicating that they are aware of the risk. However, it is still not
132 appropriate to assume that they have opted into taking a similar risk
133 on the next set of prerelease versions.
134
135 Prerelease Identifiers
136 The method .inc takes an additional identifier string argument that
137 will append the value of the string as a prerelease identifier:
138
139 semver.inc('1.2.3', 'prerelease', 'beta')
140 // '1.2.4-beta.0'
141
142 command-line example:
143
144 $ semver 1.2.3 -i prerelease --preid beta
145 1.2.4-beta.0
146
147 Which then can be used to increment further:
148
149 $ semver 1.2.4-beta.0 -i prerelease
150 1.2.4-beta.1
151
152 Advanced Range Syntax
153 Advanced range syntax desugars to primitive comparators in determinis‐
154 tic ways.
155
156 Advanced ranges may be combined in the same way as primitive compara‐
157 tors using white space or ||.
158
159 Hyphen Ranges X.Y.Z - A.B.C
160 Specifies an inclusive set.
161
162 · 1.2.3 - 2.3.4 := >=1.2.3 <=2.3.4
163
164
165 If a partial version is provided as the first version in the inclusive
166 range, then the missing pieces are replaced with zeroes.
167
168 · 1.2 - 2.3.4 := >=1.2.0 <=2.3.4
169
170
171 If a partial version is provided as the second version in the inclusive
172 range, then all versions that start with the supplied parts of the
173 tuple are accepted, but nothing that would be greater than the provided
174 tuple parts.
175
176 · 1.2.3 - 2.3 := >=1.2.3 <2.4.0
177
178 · 1.2.3 - 2 := >=1.2.3 <3.0.0
179
180
181 X-Ranges 1.2.x 1.X 1.2.* *
182 Any of X, x, or * may be used to "stand in" for one of the numeric val‐
183 ues in the [major, minor, patch] tuple.
184
185 · * := >=0.0.0 (Any version satisfies)
186
187 · 1.x := >=1.0.0 <2.0.0 (Matching major version)
188
189 · 1.2.x := >=1.2.0 <1.3.0 (Matching major and minor versions)
190
191
192 A partial version range is treated as an X-Range, so the special char‐
193 acter is in fact optional.
194
195 · "" (empty string) := * := >=0.0.0
196
197 · 1 := 1.x.x := >=1.0.0 <2.0.0
198
199 · 1.2 := 1.2.x := >=1.2.0 <1.3.0
200
201
202 Tilde Ranges ~1.2.3 ~1.2 ~1
203 Allows patch-level changes if a minor version is specified on the com‐
204 parator. Allows minor-level changes if not.
205
206 · ~1.2.3 := >=1.2.3 <1.(2+1).0 := >=1.2.3 <1.3.0
207
208 · ~1.2 := >=1.2.0 <1.(2+1).0 := >=1.2.0 <1.3.0 (Same as 1.2.x)
209
210 · ~1 := >=1.0.0 <(1+1).0.0 := >=1.0.0 <2.0.0 (Same as 1.x)
211
212 · ~0.2.3 := >=0.2.3 <0.(2+1).0 := >=0.2.3 <0.3.0
213
214 · ~0.2 := >=0.2.0 <0.(2+1).0 := >=0.2.0 <0.3.0 (Same as 0.2.x)
215
216 · ~0 := >=0.0.0 <(0+1).0.0 := >=0.0.0 <1.0.0 (Same as 0.x)
217
218 · ~1.2.3-beta.2 := >=1.2.3-beta.2 <1.3.0 Note that prereleases in the
219 1.2.3 version will be allowed, if they are greater than or equal to
220 beta.2. So, 1.2.3-beta.4 would be allowed, but 1.2.4-beta.2 would
221 not, because it is a prerelease of a different [major, minor, patch]
222 tuple.
223
224
225 Caret Ranges ^1.2.3 ^0.2.5 ^0.0.4
226 Allows changes that do not modify the left-most non-zero digit in the
227 [major, minor, patch] tuple. In other words, this allows patch and
228 minor updates for versions 1.0.0 and above, patch updates for versions
229 0.X >=0.1.0, and no updates for versions 0.0.X.
230
231 Many authors treat a 0.x version as if the x were the major "break‐
232 ing-change" indicator.
233
234 Caret ranges are ideal when an author may make breaking changes between
235 0.2.4 and 0.3.0 releases, which is a common practice. However, it pre‐
236 sumes that there will not be breaking changes between 0.2.4 and 0.2.5.
237 It allows for changes that are presumed to be additive (but non-break‐
238 ing), according to commonly observed practices.
239
240 · ^1.2.3 := >=1.2.3 <2.0.0
241
242 · ^0.2.3 := >=0.2.3 <0.3.0
243
244 · ^0.0.3 := >=0.0.3 <0.0.4
245
246 · ^1.2.3-beta.2 := >=1.2.3-beta.2 <2.0.0 Note that prereleases in the
247 1.2.3 version will be allowed, if they are greater than or equal to
248 beta.2. So, 1.2.3-beta.4 would be allowed, but 1.2.4-beta.2 would
249 not, because it is a prerelease of a different [major, minor, patch]
250 tuple.
251
252 · ^0.0.3-beta := >=0.0.3-beta <0.0.4 Note that prereleases in the
253 0.0.3 version only will be allowed, if they are greater than or equal
254 to beta. So, 0.0.3-pr.2 would be allowed.
255
256
257 When parsing caret ranges, a missing patch value desugars to the number
258 0, but will allow flexibility within that value, even if the major and
259 minor versions are both 0.
260
261 · ^1.2.x := >=1.2.0 <2.0.0
262
263 · ^0.0.x := >=0.0.0 <0.1.0
264
265 · ^0.0 := >=0.0.0 <0.1.0
266
267
268 A missing minor and patch values will desugar to zero, but also allow
269 flexibility within those values, even if the major version is zero.
270
271 · ^1.x := >=1.0.0 <2.0.0
272
273 · ^0.x := >=0.0.0 <1.0.0
274
275
276 Range Grammar
277 Putting all this together, here is a Backus-Naur grammar for ranges,
278 for the benefit of parser authors:
279
280 range-set ::= range ( logical-or range ) *
281 logical-or ::= ( ' ' ) * '||' ( ' ' ) *
282 range ::= hyphen | simple ( ' ' simple ) * | ''
283 hyphen ::= partial ' - ' partial
284 simple ::= primitive | partial | tilde | caret
285 primitive ::= ( '<' | '>' | '>=' | '<=' | '=' ) partial
286 partial ::= xr ( '.' xr ( '.' xr qualifier ? )? )?
287 xr ::= 'x' | 'X' | '*' | nr
288 nr ::= '0' | ['1'-'9'] ( ['0'-'9'] ) *
289 tilde ::= '~' partial
290 caret ::= '^' partial
291 qualifier ::= ( '-' pre )? ( '+' build )?
292 pre ::= parts
293 build ::= parts
294 parts ::= part ( '.' part ) *
295 part ::= nr | [-0-9A-Za-z]+
296
298 All methods and classes take a final options object argument. All
299 options in this object are false by default. The options supported
300 are:
301
302 · loose Be more forgiving about not-quite-valid semver strings. (Any
303 resulting output will always be 100% strict compliant, of course.)
304 For backwards compatibility reasons, if the options argument is a
305 boolean value instead of an object, it is interpreted to be the loose
306 param.
307
308 · includePrerelease Set to suppress the default behavior
309 https://github.com/npm/node-semver#prerelease-tags of excluding pre‐
310 release tagged versions from ranges unless they are explicitly opted
311 into.
312
313
314 Strict-mode Comparators and Ranges will be strict about the SemVer
315 strings that they parse.
316
317 · valid(v): Return the parsed version, or null if it's not valid.
318
319 · inc(v, release): Return the version incremented by the release type
320 (major, premajor, minor, preminor, patch, prepatch, or prerelease),
321 or null if it's not valid
322
323 · premajor in one call will bump the version up to the next major ver‐
324 sion and down to a prerelease of that major version. preminor, and
325 prepatch work the same way.
326
327 · If called from a non-prerelease version, the prerelease will work the
328 same as prepatch. It increments the patch version, then makes a pre‐
329 release. If the input version is already a prerelease it simply
330 increments it.
331
332
333 · prerelease(v): Returns an array of prerelease components, or null if
334 none exist. Example: prerelease('1.2.3-alpha.1') -> ['alpha', 1]
335
336 · major(v): Return the major version number.
337
338 · minor(v): Return the minor version number.
339
340 · patch(v): Return the patch version number.
341
342 · intersects(r1, r2, loose): Return true if the two supplied ranges or
343 comparators intersect.
344
345
346 Comparison
347 · gt(v1, v2): v1 > v2
348
349 · gte(v1, v2): v1 >= v2
350
351 · lt(v1, v2): v1 < v2
352
353 · lte(v1, v2): v1 <= v2
354
355 · eq(v1, v2): v1 == v2 This is true if they're logically equivalent,
356 even if they're not the exact same string. You already know how to
357 compare strings.
358
359 · neq(v1, v2): v1 != v2 The opposite of eq.
360
361 · cmp(v1, comparator, v2): Pass in a comparison string, and it'll call
362 the corresponding function above. "===" and "!==" do simple string
363 comparison, but are included for completeness. Throws if an invalid
364 comparison string is provided.
365
366 · compare(v1, v2): Return 0 if v1 == v2, or 1 if v1 is greater, or -1
367 if v2 is greater. Sorts in ascending order if passed to
368 Array.sort().
369
370 · rcompare(v1, v2): The reverse of compare. Sorts an array of versions
371 in descending order when passed to Array.sort().
372
373 · diff(v1, v2): Returns difference between two versions by the release
374 type (major, premajor, minor, preminor, patch, prepatch, or prere‐
375 lease), or null if the versions are the same.
376
377
378 Comparators
379 · intersects(comparator): Return true if the comparators intersect
380
381
382 Ranges
383 · validRange(range): Return the valid range or null if it's not valid
384
385 · satisfies(version, range): Return true if the version satisfies the
386 range.
387
388 · maxSatisfying(versions, range): Return the highest version in the
389 list that satisfies the range, or null if none of them do.
390
391 · minSatisfying(versions, range): Return the lowest version in the list
392 that satisfies the range, or null if none of them do.
393
394 · gtr(version, range): Return true if version is greater than all the
395 versions possible in the range.
396
397 · ltr(version, range): Return true if version is less than all the ver‐
398 sions possible in the range.
399
400 · outside(version, range, hilo): Return true if the version is outside
401 the bounds of the range in either the high or low direction. The
402 hilo argument must be either the string '>' or '<'. (This is the
403 function called by gtr and ltr.)
404
405 · intersects(range): Return true if any of the ranges comparators
406 intersect
407
408
409 Note that, since ranges may be non-contiguous, a version might not be
410 greater than a range, less than a range, or satisfy a range! For exam‐
411 ple, the range 1.2 <1.2.9 || >2.0.0 would have a hole from 1.2.9 until
412 2.0.0, so the version 1.2.10 would not be greater than the range
413 (because 2.0.1 satisfies, which is higher), nor less than the range
414 (since 1.2.8 satisfies, which is lower), and it also does not satisfy
415 the range.
416
417 If you want to know if a version satisfies or does not satisfy a range,
418 use the satisfies(version, range) function.
419
420 Coercion
421 · coerce(version): Coerces a string to semver if possible
422
423
424 This aims to provide a very forgiving translation of a non-semver
425 string to semver. It looks for the first digit in a string, and con‐
426 sumes all remaining characters which satisfy at least a partial semver
427 (e.g., 1, 1.2, 1.2.3) up to the max permitted length (256 characters).
428 Longer versions are simply truncated (4.6.3.9.2-alpha2 becomes 4.6.3).
429 All surrounding text is simply ignored (v3.4 replaces v3.3.1 becomes
430 3.4.0). Only text which lacks digits will fail coercion (version one
431 is not valid). The maximum length for any semver component considered
432 for coercion is 16 characters; longer components will be ignored
433 (10000000000000000.4.7.4 becomes 4.7.4). The maximum value for any
434 semver component is Integer.MAX_SAFE_INTEGER || (2**53 - 1); higher
435 value components are invalid (9999999999999999.4.7.4 is likely
436 invalid).
437
438
439
440
441 April 2019 SEMVER(7)