Restricting npm semver rules

Daniele Esposti's Blog
, in 02 June 2015

The npm package manager uses semver to declare the version of the external dependancies of your package in a more flexible way. Unfortunately the current version of npm by default uses the caret ^ as a default prefix for package’s versions which means the required package must have the same MAJOR version but can have a different MINOR and HOTFIX versions; this can lead to a broken code if a change in the MINOR version of the dependancy introduce an incompatibility with your code. Replacing manually all the carets with the tilde ~ is tedious and error prone so we need a way to set npm to use the tilde by default.

To do that open the terminal and execute:

npm config set save-prefix '~' --save

This will set permanently the default package’s version prefix to the tilde in all the future executions of npm, keeping us safe from potential code failure caused by wrong versions of the dependancies.

Note that this doesn’t mean that you should not use the caret in you dependancy’s declarations, but you need to use it keeping in mind what are the cons. If you want to still use the caret in you project at least be sure that your code pass the tests with all the available minor versions of the dependancy declared with the caret prefix.