# Semantic Versioning (SemVer)

> The strict, three-part numbering system that tells developers exactly how risky an update is.

Subject: Package Managers | Lesson 5 of 24 | 3 min read
Interactive lesson: https://codewithanimation.in/package-managers/semantic-versioning
Previous: [Installing Packages & The npm Registry](https://codewithanimation.in/package-managers/npm-install-and-registries.md) | Next: [Version Ranges: Caret (^) vs Tilde (~)](https://codewithanimation.in/package-managers/version-ranges-caret-tilde.md)

## What is SemVer?

Semantic Versioning is a convention for communicating changes to a declared public API. npm uses SemVer-compatible versions and ranges for package resolution.

Instead of arbitrary numbers like `v1.2` or `v2026`, SemVer forces a rigid three-part format: `MAJOR.MINOR.PATCH` (for example, `1.4.2`).

The version components communicate the maintainer's compatibility intent. They do not automatically prove that an upgrade will work in your application; review and tests still matter.

## Why does it matter?

Before SemVer, updating a dependency was a gamble. A jump from `1.1` to `1.2` might include tiny bug fixes, or it might completely rewrite the API and crash your app.

SemVer creates a binding contract between the package author and the user. By following the rules, tools like `npm` can automatically pull down safe bug fixes while preventing breaking updates from destroying your production builds.

## How it works

For a package following SemVer with an established public API, MAJOR.MINOR.PATCH communicates these changes:

1. **MAJOR** version when you make incompatible API changes (breaks existing code).

2. **MINOR** version when you add functionality in a backward-compatible manner.

3. **PATCH** version when you make backward-compatible bug fixes.

You can also append pre-release tags (like `-beta.1`) or build metadata (like `+exp.sha.5114f85`) to the end. A pre-release tag tells npm the version is unstable, giving it a lower precedence than the standard release.

## Common mistake: Version Zero

Treating version `0.y.z` as stable. Under SemVer, major version zero is for initial development. Anything can change at any time, and minor bumps (like `0.2.0` to `0.3.0`) are allowed to break the API.

## Tip: Auto-bumping

Use the `npm version` command (e.g., `npm version minor`) to automatically bump your `package.json`, create a git commit, and tag the release in one step.

## Try it: SemVer Interactive Lab

Try adjusting the version components in the interactive lab below to see exactly how npm classifies the release.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/package-managers/semantic-versioning

## Check yourself

### Question 1

A library at 2.4.1 removes a documented public API. Under SemVer, which release number fits?

- A. 3.0.0
- B. 2.4.2
- C. 2.5.0
- D. 2.4.1

Correct answer: A

For an established public API, incompatible changes require a major increment. The convention communicates intent; testing is still needed.

- Why not B: A patch increment is for backward-compatible bug fixes.
- Why not C: A minor increment is for backward-compatible functionality.
- Why not D: A changed published release must receive a new version.

### Question 2

Which change normally calls for a minor increment from 1.3.2?

- A. Removing a supported function
- B. Adding a backward-compatible public feature
- C. Fixing an internal bug without changing the API contract
- D. Changing only build metadata

Correct answer: B

New compatible functionality increments the minor version, resetting patch to zero. Compatible bug fixes instead increment patch.

- Why not A: An incompatible public-API removal calls for a major release.
- Why not C: A compatible bug fix normally calls for a patch increment.
- Why not D: Build metadata does not itself mean a new public feature.

### Question 3

How does 1.0.0-beta.1 compare with 1.0.0 under SemVer precedence?

- A. The beta has higher precedence
- B. They always compare as equal
- C. The pre-release version has lower precedence
- D. The beta is the same as 2.0.0

Correct answer: C

A pre-release precedes the corresponding normal release. Version numbers express an API convention, not proof that software has no bugs.

- Why not A: The corresponding stable release takes precedence over its pre-release.
- Why not B: Pre-release identifiers affect precedence.
- Why not D: A pre-release identifier does not increment the major version.

## Remember this

- MAJOR breaks the API, MINOR adds features, PATCH fixes bugs.
- Major version 0 (e.g., 0.x.x) means the API is unstable and can break at any time.
- Pre-release tags like `-alpha.1` indicate the version is unstable and has lower precedence.
