Carrot vs Tilde
Out of curiosity, I tried googling what the caret symbol (^
) and tilde symbol (~
) mean in a package.json
file.
It turns out that if a version is specified using the caret symbol, you want to receive both patch and minor updates to the installed package. For example, if I install a package with version 4.1.5
, then I can receive updates up to versions >= 4.1.5
and < 5.0.0
.
Whereas the tilde symbol behaves differently. With it, you only want patch-level updates.
If neither a tilde nor a caret symbol is used, you’re specifying that you want the exact version.
This Stackoverflow post explains it really well: What’s the difference between tilde(~) and caret(^) in package.json?.
Resolutions
The resolutions section in package.json
is quite handy. Suppose I have two dependencies that rely on different versions of the same package - For example, Package A and Package B both depend on different versions of lodash
.
To avoid version conflicts, I can use the resolutions
field to enforce a specific version of lodash
across my entire project. This ensures that, regardless of what version each dependency requests, the one I specify in the resolutions section will be used.
See Selective dependency resolutions for more details.
Also, an important thing to note is that resolutions
is a Yarn-specific feature. If you’re using npm
, the equivalent is called overrides
. See npm equivalent of yarn resolutions? and 0036-overrides.md.