CodePark/ questions / npm & package.json
START PRACTICINGPRACTICE
Backend question · Node.js › npm & package.json

What do ^ and ~ mean in version ranges in package.json?

^ (caret) allows minor and patch updates. ~ (tilde) allows patch updates only.

Versions follow semantic versioning (semver): MAJOR.MINOR.PATCH.

  • Major: breaking changes.
  • Minor: new features that stay backward-compatible.
  • Patch: fixes that stay backward-compatible.

The ranges:

  • ^1.4.2 allows anything compatible with 1.4.2 that does not change the leftmost non-zero part: >=1.4.2 <2.0.0. This is the default that npm install writes.
  • ~1.4.2 allows patch updates only: >=1.4.2 <1.5.0.
  • A bare 1.4.2 pins that exact version.

For 0.x versions, the caret is stricter, because npm treats a minor bump as breaking. So ^0.4.2 means >=0.4.2 <0.5.0.

Ranges decide what may be installed. The lockfile decides what is actually installed.

  • Semver versions have the form MAJOR.MINOR.PATCH.
  • ^1.4.2 allows minor and patch updates, up to <2.0.0.
  • ~1.4.2 allows patch updates only, up to <1.5.0.
  • On 0.x versions, the caret only allows patch updates within that minor version. On 0.0.x versions, it pins the exact version.
  • An exact version pins it. The lockfile records the versions actually installed.