What's the Difference Between Tilde (~) and Caret (^) in a `package.json` file?
Naveera A.
—Dependencies in a package.json
file often contain a tilde (~) or caret (^) sign before the version number. What do these signs mean, and what is the difference between them?
All npm
packages must adhere to the Semantic Versioning specification.
So if a package version looks like this 2.1.4
, each of these numbers has a meaning.
4
This number refers to a patch release, which means it is a bug fix and is backward compatible.
1
.4This number refers to a minor release, which means new features have been added but it is still backward compatible.
2
.1.4This number refers to a major release, which means that it introduces major changes and may break backward compatibility.
We can specify which releases to accept while updating a package by using special signs in front of the version number in our package.json
file.
Using a tilde sign before our version number means that we can accept only a patch release when updating our package.
Using a caret (^) sign means that we can accept minor releases and patch releases, but not a major release when updating our package.
Using an asterisk means “accept all releases”, but this is not advisable as it will accept major releases and may break our code.
Let’s say we are using the lodash
package in a project. We currently have version 3.8.0
installed. Lodash announces a new release with version number 3.9.0
.
Our package.json
file looks like the following:
"dependencies": { "lodash": "~3.8.0" },
When we update our packages, the lodash
package will not update because we have specified not to accept a minor release using ~
.
In order to accept this release we will need to change the ~
to ^
like so:
"dependencies": { "lodash": "^3.8.0" },
The npm server calculator is a fun tool to master versioning numbers and ranges.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.