This sample releases a Node.js package from local npm scripts:
npm run release:majorcreates a major version bump.npm run release:minorcreates a minor version bump.npm run release:patchcreates a patch version bump.
The helper uses npm version, which updates package.json, creates the release commit, and creates the matching Git tag automatically. It then pushes the commit and tag, and creates a GitHub Release with the GitHub CLI.
scripts/release-helper.jsvalidates the release type, checks out the target branch, runsnpm version, pushes the release commit and tag, and creates the GitHub Release.package.jsonexposes local release scripts.
- Node.js and npm.
- GitHub CLI installed as
gh. - GitHub CLI authenticated with
gh auth login. - A Git remote named
origin. - A clean working tree before running a release.
Authenticate the GitHub CLI:
gh auth loginIf you use multiple GitHub accounts, switch to the account for this repository before releasing:
gh auth switch
gh auth setup-gitgh auth setup-git configures Git HTTPS operations so git fetch, git pull, and git push can use your selected GitHub CLI account.
Patch release:
npm run release:patchMinor release:
npm run release:minorMajor release:
npm run release:majorYou can also pass the type directly:
npm run release -- patchThe release target branch defaults to main. To release from another branch:
TARGET_BRANCH=develop npm run release:patchTo push the release commit and tag without creating a GitHub Release:
SKIP_GITHUB_RELEASE=1 npm run release:patch- Reads the release type from the npm script:
major,minor, orpatch. - Verifies the working tree is clean before changing any files.
- Fetches the target branch and tags from
origin. - Checks out the target branch and pulls the latest changes.
- Calculates the next version and checks that the tag does not already exist locally or on
origin. - Runs
npm version major,npm version minor, ornpm version patch. - Pushes the release commit to the target branch.
- Pushes the new version tag.
- Runs
gh release createfor the new tag unlessSKIP_GITHUB_RELEASE=1is set.
flowchart LR
A[npm run release:*] --> B[Check repo]
B --> C[npm version]
C --> D[Push commit and tag]
D --> E[Create GitHub Release]
Using npm version keeps the package version, release commit, and Git tag consistent. The helper keeps the release flow local, so you can choose the correct GitHub CLI account with gh auth switch before running the release.
The script checks for a clean working tree and existing tags before creating the release. That prevents accidental releases from uncommitted changes and avoids overwriting an existing version tag.