What is Semantic Versioning in npm?

Semantic Versioning or SemVer is a versioning scheme used by Node.js to assign version numbers to packages.

It consists of three numbers separated by periods.

Major.Minor.Patch

Patch version

Patch version includes bug fixes and does not add or remove new functionality to the API. According to the SemVer specification, patch versions are backward compatible.

If the current version of a package is 4.17.2, then the next patch release version will be 4.17.3.

Minor version

Minor version adds new functionality in a backward-compatible manner. It means, for example, anything working in version 4.17.2 will also work in 4.18.0.

When a minor version is released, the patch number is reset to 0. For example, if the current version of a package is 4.17.2, then the next minor version will be 4.18.0.

Major version

Major version deprecates functionality and adds new features in a backward incompatible manner. For example, if a function is working in version 4.17.2 might not work in version 5.0.0.

When a major version is released, both minor and patch numbers are reset to 0. For example, suppose you have a package whose current version is 4.17.2, and you want to release the next major update. In that case, the next major version will be 5.0.0.

Rule symbols in Semantic Versioning

In Semantic versioning, rule symbols are applied before the version number. These symbols are called prefixes or identifiers. There are nine rule symbols in Semantic Versioning:

  1. ^ (caret): It freezes the major version, and changes are allowed in the minor or patch version. For example, ^4.17.2 means all versions greater than or equal to 4.17.2 and less than 5.0.0 are valid.
  2. ~ (tilde): In this, major and minor numbers are fixed, and only changes in the patch version are allowed. For example, ~4.17.2 includes all versions greater than or equal to 4.17.2 and less than 4.18.0.
  3. =: It means exact version.
  4. <: Any version which is less than the one you mention. For example, <4.17.2 means versions less than 4.17.2 are valid.
  5. <=: It means any version which is less than or equal to the one you specify. Example: <=4.17.2
  6. >: Any version greater than the one you mention is valid. Example: >4.17.2
  7. >=: It includes any version greater than or equal to the provided version. Example: >=4.17.2
  8. -: You specify the range of versions using the dash sign. Example, 4.5.1 - 4.17.5
  9. ||: It is used to combine rule symbols. Example: <4.0.0 || >4.17.5 means version that are less than 4.0.0 and greater than 4.17.5 are valid.

Apart from these nine rule symbols, there is one more rule which is explained here:

x represents any number at that position.

x at Major position: * or x as the version in package.json means any version of that package.

x at Minor position: 3 or 3.x means any version greater than or equal to 3.0.0 and less than 4.0.0 is valid. It is equivalent to ^3.0.0.

x at Patch position: 3.1 or 3.1.x means any version greater than or equal to 3.1.0 and less than 3.2.0 is valid. It is equivalent to ~3.1.0.

You can visit the semantic version calculator to play with the rule symbols.

Recommended Posts