DEV Community

Chris Cook
Chris Cook

Posted on

Release NPM Package With Automatic Versioning

I know there are many ways to automate the package release workflow, but sometimes all you need is a simple one line script that takes care of versioning and publishing.

{
  "name": "my-awesome-package",
  "version": "1.0.0",
  "scripts": {
    "release": "npm version $(semver $npm_package_version -i minor) && npm publish --tag latest",
  },
  "devDependencies": {
    "semver": "^7.3.7"
  }
}
Enter fullscreen mode Exit fullscreen mode

This release script increments the version number of the package and publishes the package to the NPM registry (or other registry). To correctly increment the version number, npm's semver package automatically finds the next version number according to the specified level minor (major/minor/patch).

One can, of course, create three scripts for each semver level major, minor, and patch:

{
  "name": "my-awesome-package",
  "version": "1.0.0",
  "scripts": {
    "release:major": "npm version $(semver $npm_package_version -i major) && npm publish --tag latest",
    "release:minor": "npm version $(semver $npm_package_version -i minor) && npm publish --tag latest",
    "release:patch": "npm version $(semver $npm_package_version -i patch) && npm publish --tag latest"
  },
  "devDependencies": {
    "semver": "^7.3.7"
  }
}
Enter fullscreen mode Exit fullscreen mode

Prelease Package With User Suffix

If you are working in a team and need to release a package to test it in another project, you may want to release that package as an alpha or beta version first to avoid your colleagues accidentally installing the latest update. This intermediate version is called prerelease, like 1.0.0-1. Of course, this version could still clash with an existing version from another teammate. To make the version truly unique, we can append the username to the version suffix. This results in version numbers like 1.0.1-zirkelc.1 with zirkelc as the username.

{
  "name": "my-awesome-package",
  "version": "1.0.0",
  "scripts": {
    "release:beta": "npm version $(semver $npm_package_version -i prerelease --preid $(npm whoami) ) && npm publish --tag beta",
  },
  "devDependencies": {
    "semver": "^7.3.7"
  }
}
Enter fullscreen mode Exit fullscreen mode

The command npm whoami returns the npm username of the currently logged-in user. This works also with private registries like GitHub Registry. The flag --tag beta is required because npm publishes packages with latest tag by default. To install this prerelease version in another package, the command npm install my-awesome-package@beta must be executed with the tag beta instead of latest (default tag if omitted).

Top comments (3)

Collapse
 
pgburrack profile image
lburrack

Question just curios: Do we still need to use the semver package to increment the package version (07/31/2023)?

Can we just do npm version minor|major|patch etc?

Collapse
 
zirkelc profile image
Chris Cook

I think I have used the semver package for the prerelease scripts that append the current username as suffix. For major/minor/patch it might work without the semver package.

Collapse
 
ballcoach12 profile image
ballcoach

I copied the scripts into my package.json and ran one, but got a usage error:

npm ERR! code EUSAGE
npm ERR!
npm ERR! Bump a package version
npm ERR!
npm ERR! Usage:
npm ERR! npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]
npm ERR!
npm ERR! Options:
npm ERR! [--allow-same-version] [--no-commit-hooks] [--no-git-tag-version] [--json]
npm ERR! [--preid prerelease-id] [--sign-git-tag]
npm ERR! [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
npm ERR! [-ws|--workspaces] [--no-workspaces-update] [--include-workspace-root]
npm ERR!
npm ERR! alias: verison
npm ERR!
npm ERR! Run "npm help version" for more info


I'm running npm version 10.2.4.