Version Ranges in package.json
You'll learn how to control which package updates npm will accept using caret, tilde, and exact version specifiers.
When you list dependencies in package.json, you're not just specifying which package you need—you're defining which versions are acceptable. Version range syntax gives you fine-grained control over the trade-off between stability and getting updates.
The Three Main Specifiers
Caret (^) — The default. Accepts MINOR and PATCH updates, but blocks MAJOR changes:
{
"dependencies": {
"lodash": "^4.17.21"
}
}
This allows 4.18.0 or 4.17.22, but blocks 5.0.0. Think of it as "I trust backward-compatible updates."
Tilde (~) — More conservative. Accepts only PATCH updates within the same MINOR version:
{
"dependencies": {
"express": "~4.18.0"
}
}
This allows 4.18.1 or 4.18.2, but blocks 4.19.0 and 5.0.0. Think of it as "only bug fixes, please."
Exact version (no symbol) — Maximum control. Accepts only the specified version:
{
"dependencies": {
"react": "18.2.0"
}
}
This blocks all updates until you manually change the version number.
Choosing Your Strategy
Use caret (^) for most dependencies—you want security patches and new features. Use tilde (~) when you need extra stability or have experienced breaking changes disguised as MINOR updates. Use exact versions only when you've encountered serious compatibility issues or need absolute reproducibility.
Key Takeaway: Caret (^) allows MINOR+PATCH updates, tilde (~) allows only PATCH updates, and exact versions lock down completely—choose based on your risk tolerance and update strategy.