Auto-publishing via npm version

So, I’m supposed to be prepping for an interview right now, but I had to write this down before I forget as it is SO FUCKING COOL.

npm has an amazing feature called npm version. Using an argument your can change the semver version number in your package.json file and automatically create a commit for that new version.

$ npm version 1.1.5

Cool, huh?

Well, it gets cooler…

$ npm version patch

…will automatically increment the patch number in your package.json, meaning you don’t even have to remember it. Similarly, npm version minor and npm version major will bump the minor and major versions.

But it gets even cooler. Add the following to your package.json:

  "scripts": {
    "postversion": "git push && git push --tags && npm publish"
  }

npm will now, after already incrementing and creating a commit, create a new tag, push the new tag and publish the new version of your package!

So after I’ve made some bugfix commits, all I have to do is type

$ npm version patch

and I get a whole new published package, a commit in git and a new release tag.

Oberaffengeil!