javascript tutorial - [Solved-5 Solutions] Difference between tilt(~) and caret(^) in package .json - javascript - java script - javascript array
Problem:
After we upgraded to latest stable node and npm, We 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(~) and caret(^)?
- What is the advantages over others?
Solution 1:
~ Tilde:
- ~ fixes major and minor numbers.
- It is used when you're ready to accept bug-fixes in our dependency, but don't want any potentially incompatible changes.
- The tilde matches the most recent minor version (the middle number).
- ~1.2.3 will match all 1.2.x versions, but it will miss 1.3.0.
- Tilde (~) gives we bug fix releases
^ Caret:
- ^ fixes the major number only.
- It is used when you're closely watching our dependencies and are ready to quickly change our code if minor release will be incompatible.
- It will update we to the most recent major version (the first number).
- ^1.2.3 will match any 1.x.x release including 1.3.0, but it will hold off on 2.0.0.
- Caret (^) gives we backwards-compatible new functionality as well.
Solution 2:
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 ~ : major version is fixed, minor version is fixed, matches any build number e.g. : ~2.4.1 means it will check for 2.4.x where x is anything ^ : major version is fixed, matches any minor version, matches any build number e.g. : ^2.4.1 means it will check for 2.x.x where x is anything
Solution 3:
^ is 1.[any].[any] (latest minor version) ~ is 1.2.[any] (latest patch) A great read is this blog post on how semver applies to npm and what they're doing to make it match the semver standard
Solution 4:
~ : 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
Solution 5:
~ fixes major and minor numbers. It is used when you're ready to accept bug-fixes in our dependency, but don't want any potentially incompatible changes.
^ fixes the major number only. It is used when you're closely watching our dependencies and are ready to quickly change our code if minor release will be incompatible.
In addition to that, ^ is not supported by old npm versions, and should be used with caution.
So, ^ is a good default, but it's not perfect. WE suggest to carefully pick and configure the semver operator that is most useful to you.
^0.0.4: 0.0.4 <= accepted < 0.1.0