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

后端 未结 19 1845
温柔的废话
温柔的废话 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:50

    ~ 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)

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

    Not an answer, per se, but an observation that seems to have been overlooked.

    The description for carat ranges:

    see: https://github.com/npm/node-semver#caret-ranges-123-025-004

    Allows changes that do not modify the left-most non-zero digit in the [major, minor, patch] tuple.

    Means that ^10.2.3 matches 10.2.3 <= v < 20.0.0

    I don't think that's what they meant. Pulling in versions 11.x.x through 19.x.x will break your code.

    I think they meant left most non-zero number field. There is nothing in SemVer that requires number-fields to be single-digit.

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

    Hat matching may be considered "broken" because it wont update ^0.1.2 to 0.2.0. When the software is emerging use 0.x.y versions and hat matching will only match the last varying digit (y). This is done on purpose. The reason is that while the software is evolving the API changes rapidly: one day you have these methods and the other day you have those methods and the old ones are gone. If you don't want to break the code for people who already are using your library you go and increment the major version: e.g. 1.0.0 -> 2.0.0 -> 3.0.0. So, by the time your software is finally 100% done and full-featured it will be like version 11.0.0 and that doesn't look very meaningful, and actually looks confusing. If you were, on the other hand, using 0.1.x -> 0.2.x -> 0.3.x versions then by the time the software is finally 100% done and full-featured it is released as version 1.0.0 and it means "This release is a long-term service one, you can proceed and use this version of the library in your production code, and the author won't change everything tomorrow, or next month, and he won't abandon the package".

    The rule is: use 0.x.y versioning when your software hasn't yet matured and release it with incrementing the middle digit when your public API changes (therefore people having ^0.1.0 won't get 0.2.0 update and it won't break their code). Then, when the software matures, release it under 1.0.0 and increment the leftmost digit each time your public API changes (therefore people having ^1.0.0 won't get 2.0.0 update and it won't break their code).

    Given a version number MAJOR.MINOR.PATCH, increment the:
    
    MAJOR version when you make incompatible API changes,
    MINOR version when you add functionality in a backwards-compatible manner, and
    PATCH version when you make backwards-compatible bug fixes.
    
    0 讨论(0)
  • 2020-11-22 00:52

    carat ^ include everything greater than a particular version in the same major range.

    tilde ~ include everything greater than a particular version in the same minor range.

    For example, to specify acceptable version ranges up to 1.0.4, use the following syntax:

    • Patch releases: 1.0 or 1.0.x or ~1.0.4
    • Minor releases: 1 or 1.x or ^1.0.4
    • Major releases: * or x

    For more information on semantic versioning syntax, see the npm semver calculator.

    More from npm documentation About semantic versioning

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

    Tilde ~ matches minor version, if you have installed a package that has 1.4.2 and after your installation, versions 1.4.3 and 1.4.4 are also available if in your package.json it is used as ~1.4.2 then npm install in your project after upgrade will install 1.4.4 in your project. But there is 1.5.0 available for that package then it will not be installed by ~. It is called minor version.

    Caret ^ matches major version, if 1.4.2 package is installed in your project and after your installation 1.5.0 is released then ^ will install major version. It will not allow to install 2.1.0 if you have ^1.4.2.

    Fixed version if you don't want to change version of package on each installation then used fixed version with out any special character e.g "1.4.2"

    Latest Version * If you want to install latest version then only use * in front of package name.

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

    Tilde (~)

    major version is fixed, minor version is fixed, matches any build number

    "express": "~4.13.3" 
    

    ~4.13.3 means it will check for 4.13.x where x is anything and 4.14.0

    Caret (^)

    major version is fixed, matches any minor version, matches any build number

    "supertest": "^3.0.0"
    

    ^3.0.0 means it will check for 3.x.x where x is anything

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