Caret, Tilde or Exact Pins: Choosing Ranges for a Library's Dependencies

Applications and libraries face different trade-offs when declaring dependency ranges. An application ships a lockfile and controls its whole tree. A library's ranges are merged with everyone else's ranges inside each user's project. That difference decides which range style is kind to your users and which quietly causes duplicate copies or blocked upgrades.

What each operator means in npm

RangeAcceptsExample
^1.4.2 (caret)Same major, at or above 1.4.21.4.2 up to, not including, 2.0.0
^0.4.2 (caret, 0.x)Same minor when major is zero0.4.2 up to, not including, 0.5.0
~1.4.2 (tilde)Same minor, at or above 1.4.21.4.2 up to, not including, 1.5.0
1.4.2 (exact)Only that version1.4.2

The special case for 0.x matters: because the semver.org specification treats 0.y.z as unstable, npm's caret narrows itself to the minor. Other ecosystems have their own conventions; Cargo, for instance, treats a bare version like "1.4.2" as a caret requirement.

How each choice plays out downstream

Caret lets the user's package manager pick one version that satisfies every package in the tree, so the dependency is usually installed once. Users receive bug fixes from upstream without waiting for you. The risk is the one shown in our transitive breakage scenario: if upstream mislabels a breaking change as a minor, your users get it.

Tilde narrows that risk to patch releases only, but narrows deduplication too. If another package in the user's tree requires ^1.5.0, the two cannot share, and the user ends up with two copies. That is wasted install size at best and, for packages with shared state or instanceof checks, a subtle bug.

Exact pins in a library almost guarantee duplicates and mean every upstream security fix needs a release from you before users can have it. Users can work around this with overrides (npm) or resolutions (Yarn), but you have pushed the work onto them.

Verdict by situation

A quick test before you publish

Install your packed library into a scratch project alongside a popular sibling package, then run npm ls <dependency>. If you see two versions, look at whether your range is narrower than it needs to be. For the full theory of ranges, see npm semver ranges in practice.

Related pages