After I upgraded to latest stable node
and npm
, I tried npm install moment --save
. It saves the entry in the package.json
with the caret(^)
prefix. Previously, it was a tilde(~)
prefix.
- Why are these changes made in
npm
? - What is the difference between
tilde(~)
andcaret(^)
? - What is the advantages over others?
Semver
Allow or disallow changes
1.2.3
.^
(like head). Allows updates at the second non-zero level from the left:^0.2.3
means0.2.3 <= v < 0.3
.~
(like tail). Generally freeze right-most level or set zero if omitted:~1
means1.0.0 <= v < 2.0.0
~1.2
means1.2.0 <= v < 1.3.0
.~1.2.4
means1.2.4 <= v < 1.3.0
.0.2
means0.2 <= v < 1
. Differs from~
because:0
All (hopefully) possibilities
Set starting major-level and allow updates upward
Freeze major-level
Freeze minor-level
Freeze patch-level
Disallow updates
Notice: Missing major, minor, patch or specifying
beta
without number, is the same asany
for the missing level.Notice: When you install a package wich has
0
as major level, update will only install new beta/pr level version! That's becausenpm
sets^
as default inpackage.json
and when installed version is like0.1.3
, it freezes all major/minor/patch levels.One liner explanation
The standard versioning system is major.minor.build (e.g. 2.4.1)
npm checks and fixes the version of a particular package based on these characters
e.g. : ~2.4.1 means it will check for 2.4.x where x is anything
e.g. : ^2.4.1 means it will check for 2.x.x where x is anything
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.
The tilde (~)
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 (^)
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
Tilde (~)
~4.13.3
means it will check for 4.13.x where x is anything and 4.14.0Caret (^)
^3.0.0
means it will check for 3.x.x where x is anythingNpm 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".
~ specfices to minor version releases ^ specifies to major version releases
For example if package version is 4.5.2 ,on Update ~4.5.2 will install latest 4.5.x version (MINOR VERSION) ^4.5.2 will install latest 4.x.x version (MAJOR VERSION)