NPM version is cool ! You should use it

Août 09, 2016

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

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

Tags:

npm,

No comments

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *