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”: {
“rel…

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"
  }
}

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"
  }
}

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"
  }
}

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).


Print Share Comment Cite Upload Translate
APA
Chris | Sciencx (2024-03-28T10:25:38+00:00) » Release NPM Package With Automatic Versioning. Retrieved from https://www.scien.cx/2022/09/20/release-npm-package-with-automatic-versioning/.
MLA
" » Release NPM Package With Automatic Versioning." Chris | Sciencx - Tuesday September 20, 2022, https://www.scien.cx/2022/09/20/release-npm-package-with-automatic-versioning/
HARVARD
Chris | Sciencx Tuesday September 20, 2022 » Release NPM Package With Automatic Versioning., viewed 2024-03-28T10:25:38+00:00,<https://www.scien.cx/2022/09/20/release-npm-package-with-automatic-versioning/>
VANCOUVER
Chris | Sciencx - » Release NPM Package With Automatic Versioning. [Internet]. [Accessed 2024-03-28T10:25:38+00:00]. Available from: https://www.scien.cx/2022/09/20/release-npm-package-with-automatic-versioning/
CHICAGO
" » Release NPM Package With Automatic Versioning." Chris | Sciencx - Accessed 2024-03-28T10:25:38+00:00. https://www.scien.cx/2022/09/20/release-npm-package-with-automatic-versioning/
IEEE
" » Release NPM Package With Automatic Versioning." Chris | Sciencx [Online]. Available: https://www.scien.cx/2022/09/20/release-npm-package-with-automatic-versioning/. [Accessed: 2024-03-28T10:25:38+00:00]
rf:citation
» Release NPM Package With Automatic Versioning | Chris | Sciencx | https://www.scien.cx/2022/09/20/release-npm-package-with-automatic-versioning/ | 2024-03-28T10:25:38+00:00
https://github.com/addpipe/simple-recorderjs-demo