What's the difference between tilde(~) and caret(^) in 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
I would like to add the official npmjs documentation as well which describes all methods for version specificity including the ones referred to in the question -
https://docs.npmjs.com/files/package.json
https://docs.npmjs.com/misc/semver#x-ranges-12x-1x-12-
~version
"Approximately equivalent to version" See npm semver - Tilde Ranges & semver (7)^version
"Compatible with version" See npm semver - Caret Ranges & semver (7)version
Must match version exactly>version
Must be greater than version>=version
etc<version
<=version
1.2.x
1.2.0, 1.2.1, etc., but not 1.3.0http://sometarballurl
(this may be the URL of a tarball which will be downloaded and installed locally*
Matches any versionlatest
Obtains latest release
The above list is not exhaustive. Other version specifiers include GitHub urls and GitHub user repo's, local paths and packages with specific npm tags
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".