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

· · 4842 views

When I create any project in Vue or React, the package's version in the package.json is prefixed with caret (^) or tilde (~). The same thing happens when I install a new package, it is listed in the package.json with caret (^) or tilde (~) prefix.

I don't understand the reason for it, and what is the difference between the caret (^) or tilde (~)?

0
2 Answers

For that first, you have to understand Semantic Versioning. It is divided into three sections separated by a dot.

1.0.2
major.minor.patch

Major, minor, and patch represent the different releases of a package.

  • MAJOR version when you make incompatible API changes,
  • MINOR version when you add functionality in a backward-compatible manner, and
  • PATCH version when you make backward-compatible bug fixes.

~version "Approximately equivalent to version", will update you to all future patch versions, without incrementing the minor version. it means to install version 1.0.2 or the latest patch version such as 1.0.4.

^version "Compatible with version", will update you to all future minor/patch versions, without incrementing the major version. It means to install version 1.0.2 or the latest minor or patch version such as 1.1.0.

0

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

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

Allow or disallow changes

  • Pin version:1.2.3.
  • Use ^(like head). Allows updates at the second non-zero level from the left: ^0.2.3 means 0.2.3 <= v < 0.3.
  • Use ~ (like tail). Generally freeze right-most level or set zero if omitted:
  • ~1 means 1.0.0 <= v < 2.0.0
  • ~1.2 means 1.2.0 <= v < 1.3.0.
  • ~1.2.4 means 1.2.4 <= v < 1.3.0.
0

Please login or create new account to participate in this conversation.