Semantic versioning in Node.js
Europe’s developer-focused job platform
Let companies apply to you
Developer-focused, salary and tech stack upfront.
Just one profile, no job applications!
This article is based on Node v16.15.1 and NPM 8.11.0.
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). Understanding semantic versioning plays a significant role in defining the way we build software.
💰 The Pragmatic Programmer: journey to mastery. 💰 One of the best books in software development, sold over 200,000 times.
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".
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 summarizes it like this:
Given a version number MAJOR.MINOR.PATCH, increment the:
Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
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.
"*"The simplest semver range, which accepts any version. Default is the latest.
"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.0 <1.3.0"is similar to
"<1.0.0"only accepts versions in the range
|| operator you can also combine versions
< 2.1 || > 1.9.
Node.js has introduced shorthand ranges operators
~ (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
^(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
Thanks for reading and if you have any questions, use the comment function or send me a message @mariokandut.
If you want to know more about Node, have a look at these Node Tutorials.
Never miss an article.