← Back to Node.js, page 2

nodejs-0024 · Easy fresher

How does semantic versioning work, and what do ^ and ~ mean in package.json?

Semver uses MAJOR.MINOR.PATCH. A patch fixes bugs, a minor adds features without breaking, and a major may break things. ^1.2.3 allows changes that keep the left-most non-zero digit (>=1.2.3 <2.0.0). ~1.2.3 allows only patch updates (>=1.2.3 <1.3.0).

Full answer

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

  • Increase PATCH for a backward compatible bug fix.
  • Increase MINOR for a backward compatible new feature.
  • Increase MAJOR for a change that can break users.

Range operators in package.json:

  • 1.2.3: exactly this version.
  • ~1.2.3: patch updates only. Same as >=1.2.3 <1.3.0.
  • ^1.2.3: minor and patch updates. Same as >=1.2.3 <2.0.0. This is the default npm writes.
  • >=1.2.3, <2.0.0, 1.x and *: comparison and wildcard ranges.

Versions below 1.0.0 are treated as unstable, so the caret is stricter: ^0.2.3 means >=0.2.3 <0.3.0, and ^0.0.3 means exactly 0.0.3.

A pre-release such as 2.0.0-beta.1 is lower than 2.0.0 and is not matched by a normal range unless the range itself names a pre-release on the same version.

Semver depends on authors following the rules. A minor release can still break you, which is why a lock file and automated tests matter. In real projects use the semver package (the one npm itself uses) instead of writing your own parser.

javascript
// A tiny teaching version of the rules. Use the 'semver' package in real code.
const parse = (v) => v.split('.').map(Number);
const compare = (a, b) => a[0] - b[0] || a[1] - b[1] || a[2] - b[2];

function limits(range) {
  const mark = range[0];
  const [major, minor, patch] = parse(/[\^~]/.test(mark) ? range.slice(1) : range);
  const low = [major, minor, patch];
  if (mark === '~') return [low, [major, minor + 1, 0]];
  if (mark === '^') {
    if (major > 0) return [low, [major + 1, 0, 0]];
    if (minor > 0) return [low, [0, minor + 1, 0]];
    return [low, [0, 0, patch + 1]];
  }
  return [low, [major, minor, patch + 1]]; // exact version
}

function satisfies(version, range) {
  const [low, high] = limits(range);
  const v = parse(version);
  return compare(v, low) >= 0 && compare(v, high) < 0;
}

const checks = [
  ['1.9.0', '^1.2.3'], ['2.0.0', '^1.2.3'], ['1.2.9', '~1.2.3'], ['1.3.0', '~1.2.3'],
  ['0.2.9', '^0.2.3'], ['0.3.0', '^0.2.3'], ['0.0.4', '^0.0.3'], ['1.2.3', '1.2.3'],
];
for (const [version, range] of checks) {
  console.log(version.padEnd(6), 'matches', range.padEnd(7), '->', satisfies(version, range));
}

The helper handles only plain x.y.z versions and the three range styles above. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): 1.9.0 matches ^1.2.3 -> true 2.0.0 matches ^1.2.3 -> false 1.2.9 matches ~1.2.3 -> true 1.3.0 matches ~1.2.3 -> false 0.2.9 matches ^0.2.3 -> true 0.3.0 matches ^0.2.3 -> false 0.0.4 matches ^0.0.3 -> false 1.2.3 matches 1.2.3 -> true

Key points

  • MAJOR.MINOR.PATCH with breaking, feature and fix meanings.
  • ^ allows minor and patch updates. ~ allows patch updates.
  • 0.x versions follow stricter caret rules.
  • A lock file protects against surprise updates.

Common mistakes

  • Thinking ^ never lets a breaking change in.
  • Treating ^0.2.3 like ^1.2.3.
  • Not committing the lock file, so versions drift between machines.
  • Using * or latest for production dependencies.

Follow-ups

  • What is a pre-release version and how is it ordered?
  • What does npm update do with a caret range?
  • How would you pin a transitive dependency?

Tips

  • Say 'left-most non-zero digit' for the caret rule.
  • Mention 0.x as the usual trap.
  • Add that a lock file, not the range, makes installs repeatable.