Node.js – update package.json version automatically


Before I do a small release and tag it, I'd like to update the package.json to reflect the new version of the program.

Is there a way to edit the file package.json automatically?

Would using a git pre-release hook help?

Best Solution

Right answer

To do so, just npm version patch =)

My old answer

There is no pre-release hook originally in git. At least, man githooks does not show it.

If you're using git-extra (, for instance, you can use a pre-release hook which is implemented by it, as you can see at It is needed only a .git/hook/ executable file which edits your package.json file. Committing, pushing and tagging will be done by the git release command.

If you're not using any extension for git, you can write a shell script (I'll name it and than you can alias it to git release with something like:

git config --global alias.release '!sh path/to/ $1'

You can, than, use git release 0.4 which will execute path/to/ 0.4. Your script can edit package.json, create the tag and push it to the server.