December 5, 2026

Caret vs Tilde in package.json: How Semantic Versioning Ranges Really Match

Shelved boxes with three-part number labels behind a rope marking a section

A package.json that says "lodash": "^4.17.0" does not ask for lodash 4.17.0. It asks for anything from 4.17.0 up to, but not including, 5.0.0, and on a fresh install npm will pick the newest version in that window. One character decides how much of the future a project agrees to accept, and the rules change in ways that surprise people once the version number starts with a zero. Running a set of ranges through npm’s own semver library, version 7.8.6, and Python’s packaging library, version 26.3, shows exactly which versions each symbol lets in.

Video: Steve Griffith – Prof3ssorSt3v3 on YouTube

The promise behind the numbers

MAJOR.MINOR.PATCH

The Semantic Versioning 2.0.0 specification defines the three numbers by what changed. A project bumps the MAJOR version “when you make incompatible API changes,” the MINOR version “when you add functionality in a backward compatible manner,” and the PATCH version “when you make backward compatible bug fixes.” Semantic Versioning 2.0.0

Version ranges are built on trusting that promise. If a library follows it, any 4.x release after 4.17.0 should work for code written against 4.17.0, because only a major release is allowed to break things.

The zero exception

The spec carves out early development: “Major version zero (0.y.z) is for initial development. Anything MAY change at any time.” A 0.x library makes no compatibility promise at all, and npm’s range rules adjust to that, as the tests below show.

Electronic components are arranged on a blue surface

What caret and tilde match

The definitions

The README of node-semver, the library npm uses, defines the tilde as allowing “patch-level changes if a minor version is specified on the comparator,” and minor-level changes if not. The caret “Allows changes that do not modify the left-most non-zero element in the [major, minor, patch] tuple.” npm/node-semver

The test

Each range was checked against the same list of versions using semver.satisfies(), and semver.validRange() printed what each range expands to:

Range Expands to Matches from the test list
^1.2.3 >=1.2.3 <2.0.0-0 1.2.3, 1.2.9, 1.3.0, 1.9.9
~1.2.3 >=1.2.3 <1.3.0-0 1.2.3, 1.2.9
1.2.x >=1.2.0 <1.3.0-0 1.2.3, 1.2.9
>=1.2.3 <2.0.0 >=1.2.3 <2.0.0 1.2.3, 1.2.9, 1.3.0, 1.9.9
* * every stable version

On a 1.x package the difference is simple. Caret takes new functionality and fixes. Tilde takes fixes only.

The -0 at the end of each upper bound is the library’s way of excluding prereleases of the next version, so <2.0.0-0 keeps out 2.0.0-beta.1 as well as 2.0.0 itself.

Picking the newest

Asked for the highest matching version from the same list, semver.maxSatisfying() returned 1.9.9 for ^1.2.3 and 1.2.9 for ~1.2.3. That is the behavior npm applies against the real registry: the newest version inside the window wins.

Below 1.0.0 the rules tighten

Caret on 0.x

Because caret protects the left-most non-zero number, it narrows as the leading digits drop to zero:

Range Expands to Matches from the test list
^0.2.3 >=0.2.3 <0.3.0-0 0.2.3, 0.2.9
~0.2.3 >=0.2.3 <0.3.0-0 0.2.3, 0.2.9
^0.0.3 >=0.0.3 <0.0.4-0 none of the others

For a 0.2.x package, caret and tilde become identical. Neither accepts 0.3.0, because under semver rules a 0.3.0 release is allowed to break 0.2.x code.

The 0.0.x case

At ^0.0.3, the range pins the version exactly. A direct check confirmed that semver.satisfies('0.0.4', '^0.0.3') returns false. A project depending on a very young library with a caret will never receive its updates automatically, which is either a safety feature or a source of confusion depending on whether anyone noticed.

Open electrical box mounted on a pole outdoors

Other range forms worth knowing

Shorthand that expands to caret or tilde

Several shorter forms expand to the same windows. Run through validRange(), a bare 1 became >=1.0.0 <2.0.0-0, and so did 1.x. That is the caret window for a 1.x package, written without a symbol. A tilde with only a major number, ~1, expanded to the same thing, matching the README’s rule that tilde allows minor-level changes when no minor version is given. A two-part caret, ^1.2, became >=1.2.0 <2.0.0-0, and a two-part tilde, ~1.2, became >=1.2.0 <1.3.0-0.

Hyphen ranges

The hyphen form reads like plain English but has a quirk at the top end. 1.2.3 - 2.3.4 expanded to >=1.2.3 <=2.3.4, an inclusive range. Leave off the patch number on the right, as in 1.2.3 - 2.3, and the result became >=1.2.3 <2.4.0-0: the partial upper version is treated as “anything in 2.3.x,” not as 2.3.0.

Combining ranges

The || operator joins alternatives. ^1.2.3 || ^2.0.0 expanded to two separate windows and matched both 1.9.9 and 2.0.0 in the test list. Libraries use this form to declare support for two major versions of a peer dependency at once, during the period when a new major release is out but many users have not moved yet.

Strict parsing

The library is also strict about the version strings themselves. semver.valid('v1.2.3') returned 1.2.3, stripping the leading v, but semver.valid('1.2') returned null, because a full semver version needs all three numbers. A separate coerce() function turned v1.2 into 1.2.0. Build scripts that read version numbers from git tags or file names often need that extra step.

Prereleases are excluded by default

The rule

A beta or release candidate does not sneak into an ordinary range. The test checked whether 1.3.0-rc.1 satisfies ^1.2.3. Numerically it sits inside the window, but the answer was false.

The node-semver README explains that a version with a prerelease tag “will only be allowed to satisfy comparator sets if at least one comparator with the same [major, minor, patch] tuple also has a prerelease tag.” The range ^1.2.3 has no prerelease tag, so no prerelease qualifies.

Opting in

Two ways around the rule showed up in testing. Passing {includePrerelease: true} to satisfies() made 1.3.0-rc.1 match ^1.2.3. And a range that itself names a prerelease admits other prereleases of that same version only: ^1.2.3-beta.2 accepted 1.2.3-beta.4 but rejected 1.2.4-beta.1. The Semantic Versioning spec also states that “Pre-release versions have a lower precedence than the associated normal version,” so 1.2.3-beta.4 sorts below 1.2.3.

Window with plants and basket on a pink wall

What npm writes to package.json

The default prefix

Running npm install lodash@^4.17.0 in an empty project did not save that range. npm resolved the newest matching version, 4.18.1, and wrote "lodash": "^4.18.1" to package.json. The lockfile recorded exactly 4.18.1.

The npm configuration documentation lists save-prefix with a default of "^", used to “Configure how versions of packages installed to a package.json file via --save or --save-dev get prefixed.” npm docs, config A separate save-exact option, false by default, saves an exact version with no prefix at all.

Ranges versus locks

The range in package.json says what a project will accept. The lockfile says what it actually got. A teammate installing from the same lockfile receives 4.18.1 even if 4.19.0 has since been published, while someone deleting the lockfile and reinstalling would get the newest 4.x. Many dependency bugs that appear on one machine and not another come from that gap, not from the caret itself.

Choosing a symbol

Caret is a reasonable default for well-maintained libraries that follow semver. Tilde suits a dependency that has broken things in minor releases before. An exact version, through save-exact or by removing the symbol, makes sense for tools where any change needs review. None of these replace committing the lockfile.

Python’s version of the same idea

The compatible release operator

Python packaging has its own range syntax, defined in the version specifiers specification. The ~= operator, called compatible release, “matches any candidate version that is expected to be compatible with the specified version.” The spec gives ~= 2.2 as equivalent to >= 2.2, == 2.*, and ~= 1.4.5 as equivalent to >= 1.4.5, == 1.4.*. Python Packaging User Guide, Version specifiers

Tested against the same versions

The packaging library’s SpecifierSet gave:

Specifier Matches
~=1.2.3 1.2.3, 1.2.9
~=1.2 1.2.3, 1.2.9, 1.3.0, 1.9.9
==1.2.* 1.2.3, 1.2.9
>=1.2.3,<2 1.2.3, 1.2.9, 1.3.0, 1.9.9

So Python’s ~=1.2.3 behaves like npm’s tilde, and ~=1.2, with one fewer segment, behaves like npm’s caret on a 1.x package. The number of segments written decides how wide the window is. The spec also says the operator “MUST NOT be used with a single segment version number such as ~=1.” The library enforces that: SpecifierSet('~=1') raised InvalidSpecifier: Invalid specifier: '~=1' instead of guessing what was meant. A requirements file with that line would fail to parse rather than quietly accept every 1.x release, which is the safer of the two possible behaviors for a tool that decides what code gets installed.

The same tilde, two meanings

That makes the tilde a trap for anyone switching languages. In npm, ~1.2 and ~1.2.3 both stay within 1.2.x. In Python, ~=1.2 reaches all the way to 1.9.9 while ~=1.2.3 stays at 1.2.x. Writing the explicit form, such as >=1.2.3,<2 in Python or >=1.2.3 <2.0.0 in npm, avoids the question entirely.

Sources

  • Semantic Versioning 2.0.0: https://semver.org/
  • npm, node-semver README: https://github.com/npm/node-semver
  • npm Docs, config (save-prefix, save-exact): https://docs.npmjs.com/cli/v10/using-npm/config
  • Python Packaging User Guide, Version specifiers: https://packaging.python.org/en/latest/specifications/version-specifiers/

920 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *