Version Ranges: Caret (^) vs Tilde (~)
How npm decides which version to install when a package is updated.
What are version ranges?
When you run npm install react, npm doesn't lock your package.json to the exact version it downloaded (e.g., 18.2.0). Instead, it adds a version range (e.g., ^18.2.0).
A version range tells npm how aggressive it should be about automatically upgrading your dependencies when new versions are published to the registry.
The two most common range specifiers are the Caret (^) and the Tilde (~).
Why does it matter?
If every package was locked to an exact version (e.g., 18.2.0), you would never receive automatic bug fixes or security patches.
However, if ranges were too loose (e.g., *), a breaking API update could slip into your project and crash your build.
The Caret and Tilde strike a balance, leveraging the rules of SemVer to allow safe updates while blocking dangerous ones.
How they resolve
Caret preserves the leftmost nonzero component. For ^1.2.3, stable matching versions run from 1.2.3 up to, but not including, 2.0.0. For ^0.2.3 the upper bound is 0.3.0; for ^0.0.3 it is 0.0.4. These rules express compatibility assumptions, not a guarantee of bug-free upgrades.
The Tilde (~) is more conservative. If a minor version is specified, it allows ONLY PATCH updates. For ~1.2.3, it will accept 1.2.4 and 1.2.99, but will block 1.3.0.
An exact specifier such as 1.2.3 constrains that direct package requirement. It does not freeze every transitive requirement or the complete build environment; keep a lockfile for resolved dependency-tree details.
Version Resolver Simulator
Use the interactive version resolver below to see exactly which simulated releases are allowed or blocked based on your chosen range.
1. Select Range Prefix
2. Select Base Version
"dependencies": {
"lodash": "^1.2.3"
}
Resolution SimulatorTargeting ^1.2.3
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
Remember this
- For ^1.2.3, stable matching versions are at least 1.2.3 and below 2.0.0.
- For ~1.2.3, stable matching versions are at least 1.2.3 and below 1.3.0.
- An exact direct version specifier does not replace a lockfile for the transitive dependency tree.
- Caret ranges preserve the leftmost nonzero component: ^0.2.3 accepts 0.2.4, while ^0.0.3 rejects 0.0.4.
Done with this concept?
Mark it complete to track your progress. No login needed.