TIL: Simplify node project versioning with 'npm version'

I must admit this first, eventhough I've been developing in node for years I've missed this feature offered by npm. All this time whenever we need to bump the package version in package.json and package-lock.json, we used to either update the versions manually or use custom bash scripts. Today I came across this command available under npm to simplify this process.

For example if we have a package currently with version 1.2.3, we can use one of the following commands to bump the version

# bumps the version to 1.2.4
npm version patch 

# bumps the version to 1.3.0
npm version minor 

# bumps the version to 2.0.0
npm version major

There are lot more parameters we can pass to npm version like prepatch, preminor etc. You can refer to npm version for more details.

git support

There is also inbuilt support for git provided with this command. So when you execute this command in a git repo, the command will bump the version, automatically commit the changes and also attaches a tag. This might confuse some(atleast I got confused at start) as you won't find any changes in working directory nor staging area after running the command as the changes will already be commited.

You can also pass an additional argument -m 'commit message' which will be used as the commit message instead of the default message which is just the new version number.