Caret (^) and Tilde (~) in package.json

Hardeep Singh
2 min readApr 10, 2018

What does this mean for you?

Well, first you should understand the difference between the two.

In the simplest terms, the tilde matches the most recent minor version (the middle number).

~1.2.3 will match all 1.2.x versions but will miss 1.3.0.

This has been the default behaviour of ‘–save’ since the start, and you are probably already comfortable seeing it in your package.json.

The caret, on the other hand, is more relaxed.

It will update you to the most recent major version (the first number). ^1.2.3 will match any 1.x.xrelease including 1.3.0, but will hold off on 2.0.0.

NPM’s semver parser relies on this sanity, so the tilde’s behavior for matching “reasonably close” versions can remains unchanged. The caret, however, won’t work with this new rule. To maintaining compatibility, it must be similarly conservative and only match the minor version.

So for version zero software, the caret behaves like the tilde.

Both ~0.1.2 and ^0.1.2 will match the most recent 0.1 software, but ignore 0.2.x since it could be incompatible.

You probably won’t notice anything immediately, and your package.json file is still fine as it is. But next time you save a dependency you may notice a caret has crept into your package.json.

Hope you like it.

Follow me for New Articles on Angular, Mongodb, Nodejs.

Stay Connected ;)

--

--