Semantic Versioning using npm

By

Learn Semantic Versioning in npm: how the major.minor.patch numbers work and how the ^ and ~ range symbols control which versions npm update will install.

~~~

If there’s one great thing in Node.js packages, is that all agreed on using Semantic Versioning for their version numbering.

The Semantic Versioning concept is simple: all versions have 3 digits: x.y.z.

When you make a new release, you don’t just up a number as you please, but you have rules:

The convention is adopted all across programming languages, and it is very important that every npm package adheres to it, because the whole system depends on that.

Why is that so important?

Because npm set some rules we can use in the package.json file to choose which versions it can update our packages to, when we run npm update (more on that in how to update all the Node dependencies).

The rules use those symbols:

Let’s see those rules in detail:

The ^ works differently on 0.x versions. The rule is that it never changes the left-most number that isn’t zero. So ^0.13.0 only allows patch releases like 0.13.1, not 0.14.0, and ^0.0.4 allows only 0.0.4.

^ is the one you’ll see most often, because npm install <package-name> saves new dependencies in package.json with a ^ range by default.

You can combine some of those notations, for example use 1.0.0 || >=1.1.0 <1.2.0 to either use 1.0.0 or one release from 1.1.0 up, but lower than 1.2.0.

There are other rules, too:

I also built a free semver advisor that tells you which version number to bump and explains what the ^ and ~ ranges allow.

Tagged: Node.js · All topics

Want me to talk about your product? You can sponsor this site.

~~~

Related posts about node: