After I upgraded to latest stable node
and npm
, I tried npm install moment --save
. It saves the entry in the package.json
See the NPM docs and semver docs:
~version
“Approximately equivalent to version”, will update you to all future patch versions, without incrementing the minor version. ~1.2.3
will use releases from 1.2.3 to <1.3.0.
^version
“Compatible with version”, will update you to all future minor/patch versions, without incrementing the major version. ^2.3.4
will use releases from 2.3.4 to <3.0.0.
See Comments below for exceptions, in particular for pre-one versions, such as ^0.2.3
semver is separate in to 3 major sections which is broken by dots.
major.minor.patch
1.0.0
These different major, minor and patch are using to identify different releases. tide (~) and caret (^) are using to identify which minor and patch version to be used in package versioning.
~1.0.1
Install 1.0.1 or **latest patch versions** such as 1.0.2 ,1.0.5
^1.0.1
Install 1.0.1 or **latest patch and minor versions** such as 1.0.2 ,1.1.0 ,1.1.1
~ Tilde:
~
freezes major and minor numbers.^ Caret:
^
freezes the major number only.~
: Reasonably close to
~1.1.5: 1.1.0 <= accepted < 1.2.0
^
: Compatible with
^1.1.5: 1.1.5 <= accepted < 2.0.0
^0.1.3: 0.1.3 <= accepted < 0.2.0
^0.0.4: 0.0.4 <= accepted < 0.1.0
You probably have seen the tilde (~) and caret (^) in the package.json. What is the difference between them?
When you do npm install moment --save, It saves the entry in the package.json with the caret (^) prefix.
In the simplest terms, the tilde (~) matches the most recent minor version (the middle number). ~1.2.3 will match all 1.2.x versions but will miss 1.3.0.
The caret (^), on the other hand, is more relaxed. It will update you to the most recent major version (the first number). ^1.2.3 will match any 1.x.x release including 1.3.0, but will hold off on 2.0.0.
Reference: https://medium.com/@Hardy2151/caret-and-tilde-in-package-json-57f1cbbe347b
npm allows installing newer version of a package than the one specified. Using tilde (~
) gives you bug fix releases and caret (^
) gives you backwards-compatible new functionality as well.
The problem is old versions usually don't receive bug fixes that much, so npm uses caret (^
) as the default for --save
.
According to: "Semver explained - why there's a caret (^) in my package.json?".
Note that the rules apply to versions above 1.0.0 and not every project follows semantic versioning. For versions 0.x.x the caret allows only patch updates, i.e., it behaves the same as the tilde. See "Caret Ranges"
Here's a visual explanation of the concepts:
Source: "Semantic Versioning Cheatsheet".