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

后端 未结 19 1767
温柔的废话
温柔的废话 2020-11-22 00:31

After I upgraded to latest stable node and npm, I tried npm install moment --save. It saves the entry in the package.json

相关标签:
19条回答
  • 2020-11-22 00:39

    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

    0 讨论(0)
  • 2020-11-22 00:41

    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
    
    0 讨论(0)
  • 2020-11-22 00:45

    ~ Tilde:

    • ~ freezes major and minor numbers.
    • It is used when you're ready to accept bug-fixes in your 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 you bug fix releases

    ^ Caret:

    • ^ freezes the major number only.
    • It is used when you're closely watching your dependencies and are ready to quickly change your code if minor release will be incompatible.
    • 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 it will hold off on 2.0.0.
    • Caret (^) gives you backwards-compatible new functionality as well.
    0 讨论(0)
  • 2020-11-22 00:47

    ~ : 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
    
    0 讨论(0)
  • 2020-11-22 00:47

    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

    0 讨论(0)
  • 2020-11-22 00:48

    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:

    semver diagram

    Source: "Semantic Versioning Cheatsheet".

    0 讨论(0)
提交回复
热议问题