← Back tothis vs that

^a.b.c vs ~a.b.c in package.json file

Written byPhuoc Nguyen
Created
29 Aug, 2023
Category
JavaScript
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

If you found this post helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

Questions? 🙋

Do you have any questions about front-end development? If so, feel free to create a new issue on GitHub using the button below. I'm happy to help with any topic you'd like to learn more about, even beyond what's covered in this post.
While I have a long list of upcoming topics, I'm always eager to prioritize your questions and ideas for future content. Let's learn and grow together! Sharing knowledge is the best way to elevate ourselves 🥷.
Ask me questions

Recent posts ⚡

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