Skip to main content
· 8 min read

SemVer Explained: Caret (^) vs Tilde (~) in package.json

Written by Muhammad Ali · Senior Full Stack Software Engineer

Personal website: codedbymali.com

Learn what ^1.2.3, ~1.2.3, 1.x and * mean in package.json, why ^0.x is different, and test any npm version range with our free SemVer checker.

You open package.json and see "react": "^18.2.0" next to "lodash": "~4.17.21". Then one day npm install pulls a new version and your build breaks — or it refuses to install a version you thought was allowed. The reason is almost always the small symbol in front of the version number.

This guide explains how semantic versioning (SemVer) ranges work, what ^ and ~ really allow, and the traps around 0.x and prerelease versions. To test any range instantly, use our free SemVer Range Checker.

What is semantic versioning?

A SemVer version has three numbers: MAJOR.MINOR.PATCH, for example 2.5.1.

  • MAJOR goes up when there are breaking changes.
  • MINOR goes up when new features are added in a backward-compatible way.
  • PATCH goes up for bug fixes only.

A version can also have a prerelease tag, like 3.0.0-beta.1 or 1.6.0-rc.2. Prereleases come before the normal release: 3.0.0-beta.1 < 3.0.0.

Caret (^): "same major version"

^1.2.3 means any version from 1.2.3 up to, but not including, 2.0.0.

So 1.2.4, 1.5.0 and 1.99.0 are allowed, but 2.0.0 is not. This is npm's default when you run npm install some-package, because new minor and patch versions should not break your code.

Tilde (~): "same minor version"

~1.2.3 means any version from 1.2.3 up to, but not including, 1.3.0.

Only patch updates are allowed: 1.2.4 and 1.2.9 match, 1.3.0 does not. Use tilde when a package has broken things in minor releases before and you only want bug fixes.

Worked example

Say your range is ^1.2.3 and these versions exist on npm:

Version Matches ^1.2.3? Why
1.2.2 ❌ below the minimum 1.2.3
1.2.3 ✅ the minimum itself
1.5.0 ✅ same major, newer minor
1.6.0-rc.1 ❌ prereleases are skipped by default
1.9.9 ✅ highest 1.x, this is what npm installs
2.0.0 ❌ new major, may have breaking changes

npm will install 1.9.9, the highest version that matches. Paste the same range and list into the SemVer Range Checker and it shows this table, the reason for each row, and the highest match.

SemVer range cheat sheet

Range Means Expands to
1.2.3 exactly this version =1.2.3
^1.2.3 same major >=1.2.3 <2.0.0
~1.2.3 same minor >=1.2.3 <1.3.0
1.2.x or 1.2 any patch of 1.2 >=1.2.0 <1.3.0
1.x or 1 any 1.x >=1.0.0 <2.0.0
~1 same as 1.x >=1.0.0 <2.0.0
^0.2.3 same minor (below 1.0) >=0.2.3 <0.3.0
^0.0.3 this exact patch >=0.0.3 <0.0.4
1.2.3 - 2.3.4 inclusive range >=1.2.3 <=2.3.4
^1.0.0 || ^2.0.0 either one 1.x or 2.x
* anything every version (risky)

The ^0.x trap

Before version 1.0.0, SemVer says anything can break. So npm makes the caret stricter:

  • ^0.2.3 allows only 0.2.x (not 0.3.0).
  • ^0.0.3 allows only 0.0.3.

If a 0.x package seems "stuck" and won't update, this is why. Bump the range by hand when you're ready.

Why prereleases don't match

^1.2.3 does not match 1.6.0-rc.1, even though the number looks inside the range. npm only matches a prerelease if the range itself has a prerelease on the same major.minor.patch — for example ^1.6.0-rc.0. This stops beta versions from sneaking into your app. In the checker, turn on Include prereleases to see what would happen without this rule.

Bumping your own package version

When you publish your own package, pick the bump by the kind of change:

npm version patch       # 1.4.2 -> 1.4.3  (bug fix)
npm version minor       # 1.4.2 -> 1.5.0  (new feature)
npm version major       # 1.4.2 -> 2.0.0  (breaking change)
npm version prerelease --preid=beta   # 1.4.2 -> 1.4.3-beta.0

The Bump tab in the SemVer Range Checker shows the next version and the right command.

Common mistakes

  • Using * or latest in package.json. The next major can break your app on any fresh install. Use ^ with a real version.
  • Thinking ^ and ~ are the same. ^ allows new minors, ~ doesn't.
  • Sorting versions as text. As text, 1.10.0 comes before 1.9.0. Use a SemVer-aware sort (the Compare & Sort tab does this).
  • Writing v1.2.3 in ranges. npm strips the v, but other tools may not. Leave it out.
  • Forgetting the lockfile. Ranges decide what can be installed; package-lock.json decides what is installed. Commit the lockfile.

FAQ

What is the difference between ^ and ~ in package.json? ^1.2.3 allows any 1.x.x from 1.2.3 up. ~1.2.3 allows only 1.2.x from 1.2.3 up.

Should I pin exact versions? For apps, a lockfile already pins exact versions, so ^ is usually fine. Pin exact versions when a package often breaks things or when you need fully repeatable builds without a lockfile.

What does 1.x mean? Any version with major 1: >=1.0.0 <2.0.0. It is the same as ^1.0.0.

Why won't npm install 2.0.0 when I have ^1.5.0? Because a new major version may contain breaking changes, and ^ never crosses a major. Change the range to ^2.0.0 on purpose after you read the changelog.

Is the SemVer checker private? Yes. It runs 100% in your browser using the same semver library as npm. Nothing is uploaded.


Try it now: SemVer Range Checker →

Related tools: JSON Validator, JSON Diff, Line Sorter, Dockerfile Generator, .env Config Checker.

SemVer Range Checker