The npm blog has been discontinued.
Updates from the npm team are now published on the GitHub Blog and the GitHub Changelog.
Introducing the npm semantic version calculator
A few weeks back, I saw a question from Kent Dodds about semver.
He was looking for a semver range for Angular that would include everything in the 1.3.x range and would also allow the beta version of 1.4.0. It turns out that there is a range which allows that, 1.3.x || >1.4.0-beta.0
, but when I asked around, very few people knew that… including folks at npm.
That’s why we’re introducing the npm semantic version calculator. With it, you can test out even the most intricate of semver ranges to see if they mean what you think they mean.
Some semver ranges you might want to try…
The basics: caret and tilde
The caret (aka hat) symbol, ^
, is used by default when you npm install --save
a package. For example, npm install --save angular
would add "angular": "^1.3.15"
to the dependencies in your package.json
. This tells npm that you want either 1.3.15
or a version that is greater than that but still starts with 1
(it is in the same major range).
The tilde can also be used, e.g. ~1.3.15
. This would mean that you want either 1.3.15
or a higher version number that starts with 1.3
(it is in the same minor range).
You can set ~
as the default on your local machine with npm config set save-prefix='~'
.
Include this or that
You can combine semver ranges using ||
. You can even chain these together to create very complex ranges, though you should only do that if you really really need to (which you probably don’t).
Include prerelease versions
By default, prerelease versions are not included in a range. This is because prerelease versions are meant to be unstable and are expected to have breaking changes. But sometimes you want to include them anyway.
To do this, you include the prerelease tag in the version number. You can also use comparators like >
to include a range of prereleases for a particular version. For example, >1.4.0-beta.0
includes all the betas for version 1.4.0
. One thing to note, the comparison for prerelease tags takes into account alphabetical order. This means that >1.4.0-c
will include any tagged version that starts with c, d, e, ...
.
Anything we’ve missed?
Let us know on twitter or in the comments if there are any other tricks that we should include, or if you have any semver questions.