/commands/semver/bump-version-number
Bump a semantic version number from the command line (major, minor, patch, prerelease)
- tool
- SemVer
- versions
- semver (npm) 7, npm 7+
- os
- linux, macos, windows
npx semver -i minor 1.4.2 prints 1.5.0. The semver CLI from npm increments a version by the level you name (major, minor, patch, premajor, preminor, prepatch, prerelease, release) and resets the lower numbers, as the SemVer rules require. To change the version inside package.json, use npm version patch (or minor, major), which also commits and tags in a git repository.
Every increment level
for level in major minor patch premajor preminor prepatch prerelease; do
printf '%-11s 1.4.2 -> %s\n' "$level" "$(npx -y semver@7 -i "$level" 1.4.2)"
done
echo "--- with a pre-release id"
printf '%-11s 1.4.2 -> %s\n' "premajor" "$(npx -y semver@7 -i premajor --preid beta 1.4.2)"
printf '%-11s 2.0.0-beta.0 -> %s\n' "prerelease" "$(npx -y semver@7 -i prerelease 2.0.0-beta.0)"
echo "--- leaving a pre-release"
for level in patch minor major release; do
printf '%-11s 2.0.0-rc.1 -> %s\n' "$level" "$(npx -y semver@7 -i "$level" 2.0.0-rc.1)"
done
echo "--- edge cases"
echo "release on 1.4.2: $(npx -y semver@7 -i release 1.4.2)"
echo "patch on 1.4 with -c: $(npx -y semver@7 -c -i patch 1.4)"Output
major 1.4.2 -> 2.0.0 minor 1.4.2 -> 1.5.0 patch 1.4.2 -> 1.4.3 premajor 1.4.2 -> 2.0.0-0 preminor 1.4.2 -> 1.5.0-0 prepatch 1.4.2 -> 1.4.3-0 prerelease 1.4.2 -> 1.4.3-0 --- with a pre-release id premajor 1.4.2 -> 2.0.0-beta.0 prerelease 2.0.0-beta.0 -> 2.0.0-beta.1 --- leaving a pre-release patch 2.0.0-rc.1 -> 2.0.0 minor 2.0.0-rc.1 -> 2.0.0 major 2.0.0-rc.1 -> 2.0.0 release 2.0.0-rc.1 -> 2.0.0 --- edge cases release on 1.4.2: null patch on 1.4 with -c: 1.4.1
Incrementing from a pre-release goes to the release it leads up to: 2.0.0-rc.1 becomes 2.0.0 for major, minor, patch and release, because 2.0.0 is the next version at each of those levels. Without --preid, pre-release levels use a bare number (2.0.0-0). release on a version that is not a pre-release prints null and still exits with 0, so check the output in scripts. -c coerces 1.4 to 1.4.0 before incrementing.
Bump the version in package.json
npm init -y >/dev/null
npm pkg set version=1.4.2
npm version minor --no-git-tag-version
npm version prerelease --preid rc --no-git-tag-version
npm pkg get versionOutput
v1.5.0 v1.5.1-rc.0 "1.5.1-rc.0"
npm version prints the new version with a v prefix and writes it to package.json (and package-lock.json when present). --no-git-tag-version skips the commit and the tag; without it, outside a git repository npm only edits the files.
Levels
| Level | From 1.4.2 | Use it for |
|---|---|---|
| major | 2.0.0 | Incompatible API changes. |
| minor | 1.5.0 | New features that stay backward compatible. |
| patch | 1.4.3 | Backward compatible bug fixes. |
| premajor / preminor / prepatch | 2.0.0-0 / 1.5.0-0 / 1.4.3-0 | Start a pre-release of the next version. |
| prerelease | 1.4.3-0 (then 1.4.3-1) | Next pre-release; bumps patch first on a release. |
| release | null (nothing to release); 2.0.0 from 2.0.0-rc.1 | Drop the pre-release part. |
| --preid rc | 1.4.3-rc.0 | Name of the pre-release identifier. |
| -n 1 | 1.4.3-rc.1 | Start the pre-release counter at 1 instead of 0. |
Pitfalls
- Two-part versions are rejected.
npx semver -i patch 1.4prints nothing and exits with 1, because1.4is not a valid version; add-c(coerce) to treat it as1.4.0. In code,semver.increturnsnull, and passing such strings to other functions throws TypeError: Invalid Version. npm versionneeds a clean working tree in a git repository, otherwise it fails withGit working directory not clean. Commit first, or use--no-git-tag-version.- A
vprefix is accepted on input but dropped from output.npx semver -i patch v1.4.2prints1.4.3; add thevback when you build a tag name. - Pre-release order is by identifier.
rc.10sorts afterrc.9because numeric identifiers compare as numbers, butrc10(no dot) compares as text and sorts beforerc9. Keep the dot.
FAQ
Why did prerelease on 1.4.2 give 1.4.3-rc.0 and not 1.4.2-rc.0?
A pre-release sorts before its release, so 1.4.2-rc.0 would be lower than the current 1.4.2. semver bumps the patch number first. Use preminor or premajor to start a pre-release of the next minor or major.
Does npm version create a git commit?
Yes, in a git repository it commits package.json (and the lockfile) and creates a tag named v<version>, unless you pass --no-git-tag-version. It refuses to run when the working tree has uncommitted changes.
How do I bump the version of a Python project?
uv version --bump patch (or minor, major) updates [project] version in pyproject.toml in a uv project; poetry version patch does the same in Poetry projects. Python versions follow PEP 440, which is close to but not the same as SemVer.