javascript tutorial - [Solved-5 Solutions] Difference between titde(~) 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:

  • 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 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 will hold off on 2.0.0.

Solution 2:

We 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 -

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

Solution 2:

~ : Reasonably close to

~1.1.5: 1.1.0 <= accepted < 1.2.0
click below button to copy the code. By JavaScript tutorial team

^: 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
click below button to copy the code. By JavaScript tutorial team

Solution 3:

  • ^ is 1.[any].[any] (latest minor version)
  • ~ is 1.2.[any] (latest patch)

Solution 4:

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 5:

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

Related Searches to javascript tutorial - Difference between titde(~) and caret(^) in package.json