Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
Update package.json version with pre-commit hook

I'm trying to automate the version bump of a Node.js project hosted on a private github repo.

The project is meant to run locally, so it's not published, neither released. People in my organization just pull the main branch and run it on their machines with yarn && yarn start.

What I want to achieve

What I want to achieve is that in the pre-commit phase a version bump is made (major, patch or minor) to the package.json of this project and committed together with the code changed according to the commit message.

All I want to do is that my PR is including the change in the package.json without me having to do it manually. I don't need releases or CI for this.

What I did successfully

I have setup Husky and commitlint in order to validate conventional commit message and it works fine.

What I tried, but failed

I tried to use semantic-release and other packages to provide this functionality, but they all imply there's a CI build or release somewhere so I'm stuck.

Any idea?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

you can user updateCommitId modules to write custom node JS code to run during pre-commit hook. Sample working code can look like below. Please upgrade as deemed required.

{
  "version": "1.12.0",
  "dependencies": {},
  "pre-commit": ["updateCommitId"],
  "scripts": {
    "updateCommitId": "node updateVersion.js"
  },
  "devDependencies": {
    "pre-commit": "^1.2.2",
    "semver": "^7.3.5",
    "simple-git": "^3.2.6"
  }
}

const semver = require("semver");
const simpleGit = require("simple-git");

const options = {
  baseDir: process.cwd(),
  binary: "git",
  maxConcurrentProcesses: 6,
};

const git = simpleGit(options);

const json = require("./package.json");

function savePackage() {
  require("fs").writeFileSync(
    process.cwd() + "/package.json",
    JSON.stringify(json, null, 2)
  );
}
async function updateVersion() {
  try {
    if (json.version) {
      const version = semver.parse(json.version);
      version.inc("minor");
      json.version = version.toString();
      savePackage();
      await git.add("package.json");
      process.exit(0);
    }
  } catch (e) {
    console.log(e);
    process.exit(1);
  }
}

updateVersion();

over 4 years ago · Santiago Trujillo Report

0

Instead of pre-commit you should use the commit-msg hook. The pre-commit hook is triggered before there is a commit message yet, but if you use the commit-msg hook, it will be triggered after there is a commit message. The hook will trigered your script and the first argument your script will receive will be the commit message itself.

Then you can write any logic on top of the commit message. You can check the message, and update version based of the message as you requested. There are many ways to it

npm version major
over 4 years ago · Santiago Trujillo Report

0

in case you have any kind of CICD pipeline (not entirely limited to git hooks scope) you can use gitversion cli

it supports version bump per commit message regex configuration example:

major-version-bump-message: '\+semver:\s?(breaking|major)'
minor-version-bump-message: '\+semver:\s?(feature|minor)'
patch-version-bump-message: '\+semver:\s?(fix|patch)'
no-bump-message: '\+semver:\s?(none|skip)'

means that a commit with message my commit msg +semver: fix will trigger a patch version bump

see this article with detailed explanation for setup.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!