Skip to content

/commands/semver/bump-version-number

Bump a semantic version number from the command line (major, minor, patch, prerelease)

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
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

bash
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

bash
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 version

Output

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

LevelFrom 1.4.2Use it for
major2.0.0Incompatible API changes.
minor1.5.0New features that stay backward compatible.
patch1.4.3Backward compatible bug fixes.
premajor / preminor / prepatch2.0.0-0 / 1.5.0-0 / 1.4.3-0Start a pre-release of the next version.
prerelease1.4.3-0 (then 1.4.3-1)Next pre-release; bumps patch first on a release.
releasenull (nothing to release); 2.0.0 from 2.0.0-rc.1Drop the pre-release part.
--preid rc1.4.3-rc.0Name of the pre-release identifier.
-n 11.4.3-rc.1Start the pre-release counter at 1 instead of 0.

Pitfalls

  • Two-part versions are rejected. npx semver -i patch 1.4 prints nothing and exits with 1, because 1.4 is not a valid version; add -c (coerce) to treat it as 1.4.0. In code, semver.inc returns null, and passing such strings to other functions throws TypeError: Invalid Version.
  • npm version needs a clean working tree in a git repository, otherwise it fails with Git working directory not clean. Commit first, or use --no-git-tag-version.
  • A v prefix is accepted on input but dropped from output. npx semver -i patch v1.4.2 prints 1.4.3; add the v back when you build a tag name.
  • Pre-release order is by identifier. rc.10 sorts after rc.9 because numeric identifiers compare as numbers, but rc10 (no dot) compares as text and sorts before rc9. 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.

Sources