how to increment the version of your node package

2020-03-07

 | 

~2 min read

 | 

217 words

Using npm package we can auto-increment the version.

To see the existing published version of a package:

$ npm view <package_name> version

To update the version, the API is for npm version is:

npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git]

The NPM docs describe this as three different types of inputs:

  1. A valid SemVer string (NPM details their implementation here)
  2. A label related to a valid SemVer String (])
  3. From the latest git tag (from-git)

There’s also an optional -m or --message flag to be included as a commit message when the version was created. It can receive the version number using the %s variable.

For example:

$ npm version patch -m "Upgrade to %s for reasons"

Intriguingly, you can also tie the publish command into npm scripts to make the process simpler. The docs, again, include a nice example of using the preversion, version, and postversion to automate much of the publishing process:

./package.json
"scripts": {
  "preversion": "npm test",
  "version": "npm run build && git add -A dist",
  "postversion": "git push && git push --tags && rm -rf build/temp"
}

Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!