What's the difference between tilde(~) and caret(^) in package.json?

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(~) and caret(^) ?
  • What is the advantages over others?

  • 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.xx release including 1.3.0, but will hold off on 2.0.0.

    http://fredkschott.com/post/2014/02/npm-no-longer-defaults-to-tildes/


    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://www.npmjs.org/doc/files/package.json.html

    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.0
  • http://sometarballurl (this may be the URL of a tarball which will be downloaded and installed locally
  • * Matches any version
  • latest 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 .

    semver表

    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.xx the caret allows only patch updates, ie it behaves the same as the tilde. See "Caret Ranges"

    Here's a visual explanation of the concepts:

    semver图

    Source: "Semantic Versioning Cheatsheet".

    链接地址: http://www.djcxy.com/p/724.html

    上一篇: 在一行中初始化一个ArrayList

    下一篇: package.json中tilde(〜)和caret(^)之间有什么区别?