I’m working on a Node.js project that uses npm for package management and Git for source control. Every time I make a commit, I want the version number in my package.json file to automatically increase. Right now I have to manually edit the version field each time which is getting annoying. Is there a way to set this up so it happens automatically? I’ve heard about Git hooks but I’m not sure if that’s the right approach or how to implement it. What’s the best practice for this kind of workflow?
i think using husky + commitizen is way better than messing with git hooks directly. its simpler to set up and vibes well with npm. husky takes care of the hooks for u and you can make it auto-increment with npm version patch b4 commits. been using it, and it’s solid!
In my experience, automating the version increment in your package.json can be efficiently handled with Git hooks. Specifically, utilizing a pre-commit hook to trigger the npm version
command is a great approach. This command will adjust the version according to the specified level—patch, minor, or major—depending on the nature of your change. This way, each commit reflects the correct version number without manual updates. Just ensure you do this selectively for significant changes to maintain clarity in your version history.
I’ve dealt with this for years - semantic-release is hands down the best solution for production. Manual version bumping and pre-commit hooks don’t even come close. It reads your commit messages and automatically figures out the right version bump using conventional commit format. Handles everything: updates package.json, creates git tags, generates changelogs. You’ll need to set up conventional commits first, but once that’s done, you’ll never mess up versioning again. Plays nice with CI/CD too.