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
As an addition to npm version
you can use the --no-git-tag-version
flag if you want a version bump but no tag or a new commit:
npm --no-git-tag-version version patch
https://docs.npmjs.com/cli/version
This is what I normally do with my projects:
npm version patch
git add *;
git commit -m "Commit message"
git push
npm publish
The first line, npm version patch
, will increase the patch version by 1 (x.x.1 to x.x.2) in package.json
. Then you add all files -- including package.json
which at that point has been modified.
Then, the usual git commit
and git push
, and finally npm publish
to publish the module.
I hope this makes sense...
Merc.
If you are using yarn you can use
yarn version --patch
This will increment package.json
version by patch (0.0.x)
, commit, and tag it with format v0.0.0
Likewise you can bump minor or major version by using --minor
or --major
When pushing to git ensure you also push the tags with --follow-tags
git push --follow-tags
You can also create a script for it
"release-it": "yarn version --patch && git push --follow-tags"
Simply run it by typing yarn release-it
npm version
is probably the correct answer. Just to give an alternative I recommend grunt-bump. It is maintained by one of the guys from angular.js.
Usage:
grunt bump
>> Version bumped to 0.0.2
grunt bump:patch
>> Version bumped to 0.0.3
grunt bump:minor
>> Version bumped to 0.1.0
grunt bump
>> Version bumped to 0.1.1
grunt bump:major
>> Version bumped to 1.0.0
If you're using grunt anyway it might be the simplest solution.
I want to add some clarity to the answers this question got.
Even thought there are some answers here that are tackling properly the problem and providing a solution, they are not the correct ones. The correct answer to this question is to use npm version
Is there a way to edit the file package.json automatically?
Yes, what you can do to make this happen is to run the npm version
command when needed, you can read more about it here npm version, but the base usage would be npm version patch
and it would add the 3rd digit order on your package.json
version (1.0.X)
Would using a git pre-release hook help?
You could configure to run the npm version
command on the pre-release hook, as you need, but that depends if that is what you need or not in your CD/CI pipe, but without the npm version
command a git pre-release
hook can't do anything "easily" with the package.json
The reason why npm version
is the correct answer is the following:
package.json
he is using npm
if he is using npm
he has access to the npm scripts
.npm scripts
he has access to the npm version
command. The other answers in which other tools are proposed are incorrect.
gulp-bump
works but requires another extra package which could create issues in the long term (point 3 of my answer)
grunt-bump
works but requires another extra package which could create issues in the long term (point 3 of my answer)
Just in case if you want to do this using an npm package semver
link
let fs = require('fs');
let semver = require('semver');
if (fs.existsSync('./package.json')) {
var package = require('./package.json');
let currentVersion = package.version;
let type = process.argv[2];
if (!['major', 'minor', 'patch'].includes(type)) {
type = 'patch';
}
let newVersion = semver.inc(package.version, type);
package.version = newVersion;
fs.writeFileSync('./package.json', JSON.stringify(package, null, 2));
console.log('Version updated', currentVersion, '=>', newVersion);
}
package.json
should look like,
{
"name": "versioning",
"version": "0.0.0",
"description": "Update version in package.json using npm script",
"main": "version.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"version": "node version.js"
},
"author": "Bhadresh Arya",
"license": "ISC",
"dependencies": {
"semver": "^7.3.2"
}
}
just pass major
, minor
, patch
argument with npm run version
. Default will be patch
.
example:
npm run version
or npm run verison patch
or npm run verison minor
or npm run version major
Git Repo