Pre-release Version Ordering in SemVer
How pre-release versions like 1.0.0-alpha.1, 1.0.0-beta.2, and 1.0.0-rc.1 are ordered. Numeric vs string identifiers and precedence rules.
Detailed Explanation
Pre-release Version Ordering
Pre-release versions are denoted by appending a hyphen and a series of dot-separated identifiers after the patch version: 1.0.0-alpha.1.
Key Rules
- Pre-release < Release:
1.0.0-alpha < 1.0.0 - Left-to-right comparison: Identifiers are compared one by one from left to right
- Numeric < String: A numeric identifier always has lower precedence than a string identifier
- Numeric comparison: Identifiers consisting of only digits are compared as integers
- String comparison: String identifiers are compared lexically (ASCII sort)
- Shorter < Longer: If all preceding identifiers are equal, the shorter set has lower precedence
Ordering Examples
From lowest to highest precedence:
1.0.0-alpha
1.0.0-alpha.1
1.0.0-alpha.beta
1.0.0-beta
1.0.0-beta.2
1.0.0-beta.11
1.0.0-rc.1
1.0.0
Why alpha.beta > alpha.1?
alphavsalpha→ equalbeta(string) vs1(numeric) → string > numeric
So 1.0.0-alpha.beta > 1.0.0-alpha.1.
Why beta.11 > beta.2?
betavsbeta→ equal11vs2→ numeric comparison: 11 > 2
So 1.0.0-beta.11 > 1.0.0-beta.2. This is different from lexical string comparison where "11" < "2".
Pre-release in Ranges
An important npm behavior: a pre-release version only satisfies a range if the range explicitly includes a pre-release on the same major.minor.patch. For example, >=1.0.0-alpha.1 will include 1.0.0-alpha.2 but the general range >=1.0.0 will not match 1.0.1-alpha.1 in npm's implementation.
Use Case
Publishing alpha/beta/RC releases of npm packages and understanding how they will be resolved by consumers, or setting up CI pipelines that handle pre-release version channels.