All posts tagged semver

I recently read Semver in node.js and npm, which aims to shed some light on the oft misunderstood operators ~ and ^ in semver. The very short version is that:

  • ~ with just a major version will match any version with that major version.
  • ~ with a major and minor version will match any patch version of the minor version.
  • ^ will always match versions between the specified version and the next major version.

So ~1 would match 1.6.2, but ~1.4 would not; ^1 would match 1.6.2, and ^1.4 would too.

A concise overview of semver can be found at semver ftw. Plenty more detail on semver in node can be found on node-semver on github. If you look closely, you’ll notice that for the ^ and ~ operators, versions below 1.0.0 are treated differently: the first non-zero component is considered to potentially introduce breaking changes. This means that ^0.2.3 would not match version 0.3.0. Some consider this a bad idea, and I would have to agree since it complicates the system and reduces the chance that developers will use it properly. The problems with major version 0 as a special case are discussed in Kill the special pre-1.0.0 meaning for ^ & ~ and 1.0.0 is not just a number

If semver seems overly complicated, there is always slimver, which describes a simplified subset of semver. Slimver excludes prerelease and build metadata and limits version numbers to 65535, which allows easy encoding as a single numeric value for easy comparison. It also simplifies the range expressions for ~ and ^ so that their meaning is identical and versions in the 0.x.x and 0.0.x range are treated the same way as others, making it far more likely that developers will get what they are expecting when using either operator.