Open In App

Difference between tilde ( ~ ) and caret ( ^ ) in package.json

Last Updated : 14 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In package.json, the tilde (~) and caret (^) symbols are used to specify the version range for dependencies, controlling how updates are handled when you run npm install or yarn install. Tilde allows only the patch version upgrades avoiding the minor updates while caret allows updates to patch as well as minor versions.

When we open our package.json file and search for the dependency property and in there we find the packages that are listed as a nested object of the dependency property package-name:package-version. Now look at the package version, we find some numbers separated by three dots. 

NPM versions follow a three-number format, separated by dots. The leftmost number signifies the major release, the second one represents the minor release, and the third number denotes the patch release of the package.

Syntax: The syntax of the npm version looks like the following.

// package-name:  Major.Minor.Patch

"express": "~4.16.3" // tilde for patch updates
"express": "^4.16.3" // caret for flexible updates

Tilde (~) notation

The tilde (~) notation is employed to match the latest patch version while freezing the major and minor versions. This notation is useful for automatically accepting bug fixes, considering that patch updates primarily address bugs.

  • Example: The ~1.2.0 will update all the future patch updates. We have to write just ~1.2.0 and all the next patch update dependencies. For example, 1.2.1, 1.2.2, 1.2.5...............1.2.x.

Note: Patch updates are very small security changes in a package that is why the ~version is approximately equivalent to the version.

Use ~ when you want to avoid minor version updates but still receive patch updates for bug fixes.

Caret (^) notation

It automatically updates both minor and patch updates.

  • Example: The ^1.2.4 will update all the future Minor and patch updates, for example, ^1.2.4 will automatically change the dependency to 1.x.x if any update occurs. 

Using caret notation it is important to look at our code regularly if it is compatible with the newest version or not.

Use ^ when you want to allow minor updates that may add features but avoid major updates that could introduce breaking changes.

Difference between tilde (~) and caret (^) in package.json

Tilde (~) notation

Caret (^) notation

Used for Approximately equivalent to version.Used for Compatible with version.
It 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.It 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
It gives you bug fix releases.It gives you backwards-compatible new functionality as well.
It will update in decimals.It will update to its latest version in numbers.
Not a default notation used by NPM.Used by NPM as default notation.
Example: ~1.0.2Example: ^1.0.2

Conclusion

The tilde (~) restricts updates to patch versions for stability, while the caret (^) allows updates to both minor and patch versions, offering more flexibility without breaking changes.


Next Article

Similar Reads