update package.json version automatically

后端 未结 12 2090
长发绾君心
长发绾君心 2020-12-02 04:37

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

12条回答
  •  臣服心动
    2020-12-02 05:14

    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:

    1. If the user is using a folder structure in which he has a package.json he is using npm if he is using npm he has access to the npm scripts.
    2. If he has access to npm scripts he has access to the npm version command.
    3. Using this command he doesn't need to install anything more in his computer or CD/CI pipe which on the long term will reduce the maintainability effort for the project, and will help with the setup

    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)

提交回复
热议问题