NPM version is cool ! You should use it

In these times where NPM scripts are becoming more and more popular, I want to show you an unknown command of NPM : npm version.

NPM comes with lots of bundled commands, I let you tap “npm –help” in your favorite terminal to show all of them.
The purpose of npm version is to change the version of a project. You know, when you open a package.json file, one of the first properties is often the current version of the project :
version property in package.json
For instance, when you increment the version because you have a fix, you can use npm version patch, if you want to update the minor version, use npm version minor or, to increment the major number use npm version major. Simple, is it not ?
Moreover, if you are in a git repository it will also automatically create a version commit and tag.

Hooks

But the most interesting thing for us, is the usage of pre and post hooks.
Indeed, like all commands in NPM, npm version has at its disposal, a pre hook executed before the command and a post hook executed after. For example, if I want to execute something after I pumped the version, I just have to add this to my package.json :
postversion in a package.json
Above, we automatically push commits and tags on remote origin after bumping the version. Better, use the preversion to run your test before tagging :
Scripts part in JS-Republic website's package.json
As you can see above, in the complete script property used in the JS-Republic website, npm test is called in preversion hook to verify if the build is not broken.

Conclusion

Simple and powerful, this command available out of the box in NPM could become your next friend for smooth and easy releases. To find more information about npm version you can go there.
By Mathieu Breton
[separator type=”” size=”” icon=”star”] [actionbox color=”default” title=”” description=”JS-REPUBLIC est une société de services spécialisée dans le développement JavaScript. Nous sommes centre de formation agréé. Retrouvez toutes nos formations techniques sur notre site partenaire dédié au Training” btn_label=”Nos formations” btn_link=”http://training.ux-republic.com” btn_color=”primary” btn_size=”big” btn_icon=”star” btn_external=”1″]