^a.b.c vs ~a.b.c in package.json file
Written byPhuoc Nguyen
Category
JavaScript
Created
29 Aug, 2023
When working with Node Package Manager (NPM), you might come across two symbols in the
`package.json`
file for specifying package versions - the caret (`^`
) and the tilde (`~`
). These symbols are used to indicate the acceptable range of package versions.But before we dive into the differences between them, let's first take a look at the standard for version naming.
#Semver
If you're using packages in NPM, you've probably heard of semver. It's a version naming system that makes it easy to understand the significance of different versions of a package.
In semver, each version is made up of three numbers separated by dots -
`MAJOR.MINOR.PATCH`
.The
`MAJOR`
number goes up when there are breaking changes to the package, the `MINOR`
number increases when new features are added without breaking anything, and the `PATCH`
number goes up when only bug fixes are included.Understanding semver can help you make informed choices about which versions of a package to use in your project and how to specify acceptable ranges of versions using
`^`
and `~`
in your package.json file.#The caret (^)
In your
`package.json`
file, if you see a caret symbol before a version number, it means that NPM is cool with accepting any version of the package that works with the specified version number. Basically, compatible versions are those that have the same major version as the specified version number, but can have any minor or patch version number.Let's say you put
`^1.2.3`
as the version number for a package in your `package.json`
file. NPM will accept any package version that has a major version of 1, but can have any minor or patch version number. This means that NPM will accept versions like `1.3.0`
, `1.4.5`
, and `1.99.99`
, but not versions like `0.9.9`
or `2.0.0`
.#The tilde (~)
When you see a tilde symbol before a version number in the
`package.json`
file, it means that NPM will accept any package version that's compatible with the specified version number. But there's a catch: it restricts the minor version number. Compatible versions must have the same major and minor version as the specified version, but can have any patch version number.For instance, if you set
`~1.2.3`
as the version number for a package in your `package.json`
file, NPM will accept any package version with a major version of 1 and a minor version of 2, but it can have any patch version number. This means that NPM will allow versions like `1.2.0`
, `1.2.4`
, and `1.2.99`
, but not versions like `1.3.0`
or `2.0.0`
.#Conclusion
To sum up, when working with NPM, the caret and tilde symbols in the
`package.json`
file are crucial to specify the acceptable range of versions for a package. The caret enables any minor or patch version, whereas the tilde restricts the minor version but allows any patch version. Understanding these symbols is critical because they can impact the compatibility and stability of your project.#See also
Questions? 🙋
Do you have any questions? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.
Recent posts ⚡
Copy the content of an element to your clipboard
01 Oct, 2023
Make a text area fit its content automatically
30 Sep, 2023
Quickly insert alternate characters while typing
30 Sep, 2023
Zebra-like background
30 Sep, 2023
Add autocomplete to your text area
28 Sep, 2023
Linear scale of a number between two ranges
28 Sep, 2023
Highlight the current line in a text area
27 Sep, 2023
Create your own custom cursor in a text area
27 Sep, 2023
Mirror a text area for improving user experience
26 Sep, 2023
Display the line numbers in a text area
24 Sep, 2023
Select a given line in a text area
24 Sep, 2023
Newsletter 🔔
If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn