How To Automatically Bump NPM Package Version
This post assumes you have a general understanding of NPM and package publishing.
If you’re tired of forgetting to bump your version number in package.json before publishing your private NPM package and getting this error:
ERR! You cannot publish over the previously published versions: 1.1.34
You can add a simple script in our package.json file to automatically bump the version and then publish the package. Add the following to have your version¹ bump from 1.1.2 to 1.1.3 before you publish to your package registry.
package.json{
"scripts": {
"pub": "npm version patch --force && npm publish"
}
}
Now when you’re ready to publish run this with the command:
npm run pub
You will get a passive warning from NPM about using — force, but it can be safely ignored. Your package version will be bumped and the package will publish.
¹ You can also use the npm-version flags of major and minor to go from 1.1 to 2.1 and 1.1 to 1.2 respectively:
"npm version major --force && npm publish" => 2.1
"npm version minor --force && npm publish" => 1.2