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
| Range | Accepts | Example |
|---|---|---|
^1.4.2 (caret) | Same major, at or above 1.4.2 | 1.4.2 up to, not including, 2.0.0 |
^0.4.2 (caret, 0.x) | Same minor when major is zero | 0.4.2 up to, not including, 0.5.0 |
~1.4.2 (tilde) | Same minor, at or above 1.4.2 | 1.4.2 up to, not including, 1.5.0 |
1.4.2 (exact) | Only that version | 1.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
- Most runtime dependencies of a published library: caret. It cooperates with the rest of the tree and lets fixes flow.
- An upstream with a history of breaking changes in minors: tilde, with a note in your changelog when you widen it.
- Development dependencies (test runners, linters): anything you like, since they are not installed for your users; your lockfile does the real pinning.
- Peer dependencies: wide ranges across majors you have tested, for example
^18.0.0 || ^19.0.0, as discussed in the peer range scenario. - Applications: the range matters less because the lockfile is authoritative; exact pins are a matter of taste.
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.