What is Semantic versioning?
Tags: node, javascript
Published: 2021-05-04 14:40:05

---
title: What is Semantic versioning?
published: true
date: 2021-02-23 00:00:00 UTC
tags: node, javascript
canonical_url: https://www.mariokandut.com/what-is-semantic-versioning-semver/
---

 **Semantic Versioning** or **semver** is a core part in software development and has also become a core part of Node.js. Semver is already embedded in the way we publish and link packages together with NPM (check out [Beginner's guide to NPM](https://www.mariokandut.com/beginners-guide-to-npm/)). Understanding semantic versioning plays a significant role in defining the way we build software.

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#what-is-semver)What is Semver?

Semver is a specification outlining a method of encoding the change between releases of a "public interface", directly into the version string. A public interface could be basically anything an application programming interface ( **API** ), a command-line interface ( **CLI** ) or a graphical user interface ( **GUI** ). Anything that depends on having predictable interactions should be versioned semantically. Semver could even be extended to physical interfaces.

**Semver is a scheme for interface versioning** for the benefit of interface consumers. Thus, if a tool has multiple interfaces, e.g. an API and a CLI, these interfaces may evolve independent versioning. Although many applications do not consider their CLI to be part of their interface when versioning, a third-party may depend on specific CLI behaviour in the same way they might depend on an API.

In simple terms - "semver is a convention to provide a meaning to versions".

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#semver-construction)Semver Construction

A `semver` version is built from three numbers separated by dots `.`. These three numbers are referred to as **major** , **minor** and **patch** (reading left to right). The combination of these numbers represent an ordered version, where each of the three numbers are also ordered.

For example: Version 1.2.3 is ordered before Version 1.4.1 and Version 0.8.19 is ordered before 1.0.0.

[Semver.org](https://www.semver.org) summarizes it like this:

Given a version number MAJOR.MINOR.PATCH, increment the:

- MAJOR version when you make incompatible API changes,
- MINOR version when you add functionality in a backwards compatible manner, and
- PATCH version when you make backwards compatible bug fixes.

Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#semver-ranges-in-node)Semver Ranges in Node

Semver is important in the Node.js ecosystem, because it's built into the way that npm manages package dependencies.

All packages published to npm are assumed to follow semver semantics. Hence, it is used by nearly every package author to define what dependency versions the package is bundled with.

One major concept is **Semver Ranges**. This concept was inspired by Bundler (Ruby ecosystem), but for a ruby application semver ranges have greater implications than in the node.js ecosystem. In a Node.js project it is essential to have a pragmatic dependency management, since it is common to use several third-party packages.

Semver ranges are basically **permitting newer version of packages to be installed automatically**. Important bug fixes/patches can be received or distributed automatically, but major changes are forbidden to be installed.

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#options-for-semver-ranges)Options for Semver Ranges

- `"*"` The simplest semver range, which accepts any version. Default is the **latest**.
- `"2"` or `"2.x.x"` Specify a specific version. `"2"` would cover all minor and patch versions.

You can specify version ranges with `-, <, <=, > and >=`. For example:

- `"1.2.3 - 2.3.4"` is the same as `">=1.2.3 <=2.3.4"`
- `">=1.2.0 <1.3.0"` is similar to `"1.2.x"`
- `"<1.0.0"` only accepts versions in the range `"0.x.x"`

With the `||` operator you can also combine versions `< 2.1 || > 1.9`.

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#shorthand-range-operators)Shorthand Range Operators

Node.js has introduced shorthand ranges operators **`~`(tilde)** and **`^`(caret)**.

**~ (tilde)** character defines a **range of acceptable PATCH versions** from the one specified up to, but not including, the next minor version. `"~1.2.3"` is similar to `">=1.2.3 <1.3.0"`.**`^`(caret)** defines a range of acceptable PATCH and MINOR versions from the ones specified up to, but not including, the next version. So `"^1.2.3"` is similar to `">=1.2.3 <2.0.0"`.

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#tldr)TL;DR

- Semver is a core part of Node.js and already integrated with NPM.
- Semver ranges are basically **permitting newer version of packages to be installed automatically**.
- There are several rules and operators to define semver ranges, and it is important to understand them.

Thanks for reading and if you have any **questions** , use the **comment** function or **send me a message** **[@mariokandut](https://www.twitter.com/mariokandut)**.

If you want to know more about **[Node](https://www.mariokandut.com/tags/node)**, have a look at these [Node Tutorials](https://www.mariokandut.com/tags/node).

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#references-and-big-thanks)References (and Big thanks):

[NodeSource](https://nodesource.com/blog/semver-a-primer/), [FlavioCopes](https://flaviocopes.com/npm-semantic-versioning/), [SemVer](https://semver.org/)