NPM Semantic Versioning

Wed Dec 16 2020 - 3 min read

Hello everyone!

I will be giving a quick walkthrough of how the npm semantic versioning works.

One of the advantages of understanding this versioning is that it helps us avoid breaking changes in our application.

code-snapshot.png

code-snapshot1.png

On the dependencies field, we see an object that lists all our dependencies. For the case in the screenshot, we see that express is a dependency.

But what does the caret and tilde along with the numbers mean?

This is all but the semantic node versioning. In this versioning, the version of a node package has 3 components specified by each of the numbers attached to the value field of the package.

For example in the above screenshot, we see that the express package has something like:

"^4.17.1" or  "~4.17.1"

Let's carefully explain what each of these numbers and the caret (sometimes tilde) sign mean.

The first number is called the Major version, the second is the Minor version while the last number is the Patch version.

The Major version is used or increased when a feature that can possibly break the functionality of the previous release, is added. So, for the above, the version becomes 5.0.0

The Minor version is used or increased when a new feature that does not break the package is added. For example, when the express team adds a new feature that does not break any API that uses it, they will simply increase the version to 4.18.0. The zero here will be explained below, but just for a quick understanding, it is zero because it is still fresh and no bug has been found yet as the patch version is used for bug-fixing

The patch version is used for bug fixing, for example, if the express team fixes a bug on the current version, the next version will be a 4.17.2

The Caret and Tilde Sign

Let's say, someone pulls our code from Github and tries to run npm install in order to install packages, what the caret character does is simply tell npm that it is interested in downloading the current version of the package so far as the major version has not changed.

For example, in the case of the express package, it simply downloads the package so far as the major version has not moved to 5. That is, if the current version is 4.19.8, npm will download it but if it is 5.0.0 or above it will not.

The syntax can also be specified as 4.x as shown in the screenshot below;

code-snapshot2.png

But for the case of the Tilde sign, we simply tell npm we are interested in the latest version as long as the major version and minor version match.

For example in the case of the express package, npm will install a package so far as the package is still within 4.17. We can alternatively write this as below

code-snapshot3.png

Please Note: There are times you may want an exact version of the package previously installed due to some breaking changes, in that case, all you need do is remove any character that preceded the versioning(either caret or tilde). As such, we have something like the screenshot below;

code-snapshot4.png

I hope this helps. Thank you for reading!

  • NodeJS
  • NPM
  • Express
  • JavaScript