Loading

Quipoin Menu

Learn • Practice • Grow

node-js / Semantic Versioning
tutorial

Semantic Versioning

Have you ever seen package versions like `^4.17.21` or `~2.5.0` and wondered what those symbols mean? That's **semantic versioning** (SemVer) – a standard way to version software. Understanding it is crucial for managing dependencies safely.

What is Semantic Versioning?

Semantic versioning is a 3-part version number: **MAJOR.MINOR.PATCH**.
  • MAJOR: Incremented when you make incompatible API changes.
  • MINOR: Incremented when you add functionality in a backward-compatible manner.
  • PATCH: Incremented when you make backward-compatible bug fixes.

Think of it as a promise: if the MAJOR version is the same, your code should still work. MINOR updates add new features without breaking anything. PATCH updates just fix bugs.

Example: Express Versions

express@4.18.2
│ │ │
│ │ └── PATCH (bug fixes)
│ └───── MINOR (new features, no breaking changes)
└──────── MAJOR (breaking changes)

Version Ranges in package.json

When you install a package, npm saves a version range in `package.json`. This determines which versions can be installed when you run `npm install` later.

PrefixMeaningExample
`^` (caret)Compatible with MAJOR version. Allows MINOR and PATCH updates.`^4.17.21` → 4.x.x (but not 5.0.0)
`~` (tilde)Approximately equivalent. Allows PATCH updates.`~4.17.21` → 4.17.x (but not 4.18.0)
No prefixExact version only.`4.17.21` → exactly that version.
`*`Any version (not recommended).`*` → latest version.
`>=1.2.3 <2.0.0`Range of versions.`>=1.2.3 <2.0.0`

Caret (^) vs Tilde (~)

The difference is subtle but important:
  • Caret (^): Allows updates that don't change the leftmost non-zero digit. So `^1.2.3` allows 1.x.x (up to 2.0.0), `^0.2.3` allows 0.2.x (up to 0.3.0), `^0.0.3` allows only 0.0.3.
  • Tilde (~): Allows patch updates only. So `~1.2.3` allows 1.2.x (up to 1.3.0), `~1.2` allows 1.2.x, `~1` allows 1.x.x.

Why Semantic Versioning Matters

  1. Predictability: You know what changes will break your code.
  2. Automated Updates: Tools like `npm update` can safely update packages within ranges.
  3. Dependency Resolution: npm can figure out compatible versions.

Checking a Package's Versions

To see all available versions of a package:
npm view express versions

To see the latest version:
npm view express version

Updating Packages

To update all packages to the latest version within the ranges specified in `package.json`:
npm update

To update to the latest major version (potentially breaking), you need to install explicitly:
npm install express@latest

Two Minute Drill

  • SemVer = MAJOR.MINOR.PATCH
  • MAJOR changes break compatibility, MINOR adds features, PATCH fixes bugs.
  • `^` allows MINOR and PATCH updates; `~` allows only PATCH updates.
  • Version ranges in `package.json` control which updates are allowed.
  • Understanding SemVer helps you manage dependencies safely.

Need more clarification?

Drop us an email at career@quipoinfotech.com