# Version Ranges: Caret (^) vs Tilde (~)

> How npm decides which version to install when a package is updated.

Subject: Package Managers | Lesson 6 of 24 | 4 min read
Interactive lesson: https://codewithanimation.in/package-managers/version-ranges-caret-tilde
Previous: [Semantic Versioning (SemVer)](https://codewithanimation.in/package-managers/semantic-versioning.md) | Next: [dependencies vs devDependencies](https://codewithanimation.in/package-managers/dependencies-vs-devdependencies.md)

## 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.

## Common mistake: Major version 0 trap

Because `0.y.z` is considered unstable under SemVer, the Caret treats it strictly. `^0.2.3` will NOT allow an upgrade to `0.3.0`, because the leftmost non-zero digit (`2`) would change. This often confuses developers who expect Caret to always allow minor updates.

## Tip: Changing the default

You can change npm's default prefix by running `npm config set save-prefix="~"`. Some teams prefer this stricter default to avoid unexpected minor updates.

## Try it: Version Resolver Simulator

Use the interactive version resolver below to see exactly which simulated releases are allowed or blocked based on your chosen range.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/package-managers/version-ranges-caret-tilde

## Check yourself

### Question 1

Which stable version satisfies ^1.2.3?

- A. 2.0.0
- B. 1.2.2
- C. 0.9.9
- D. 1.9.0

Correct answer: D

For this range, compatible stable versions are at least 1.2.3 and below 2.0.0. Prerelease matching has additional rules.

- Why not A: The next major version is outside this caret range.
- Why not B: This is below the minimum version.
- Why not C: This is below the required major/minimum version.

### Question 2

Which stable version satisfies ~1.2.3?

- A. 1.2.9
- B. 1.3.0
- C. 1.2.1
- D. 2.2.3

Correct answer: A

With all three components specified, this tilde range accepts patches from 1.2.3 up to, but not including, 1.3.0.

- Why not B: The next minor version is excluded.
- Why not C: This is below the lower bound.
- Why not D: The major version is outside the range.

### Question 3

Which statement correctly compares ^0.2.3 and ^0.0.3 for stable releases?

- A. Both ranges accept every 0.x release
- B. ^0.2.3 accepts 0.2.4; ^0.0.3 rejects 0.0.4
- C. Both accept every later patch number
- D. ^0.2.3 accepts 0.3.0

Correct answer: B

Caret preserves the leftmost nonzero component. These ranges stop before 0.3.0 and 0.0.4 respectively.

- Why not A: Caret does not permit changing the leftmost nonzero component.
- Why not C: ^0.0.3 excludes 0.0.4, despite that being a patch increment.
- Why not D: 0.3.0 changes the leftmost nonzero component here.

## 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.
