Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

husky pre-commit hook not working after adding it to package.json

I am working on a project and I want that every time I try to commit a new change, my tests are run and based on that it is decided whether the commit would happen. For this to happen I researched and found I can use husky's pre-commit hook.

I first installed husky in my project with npm i husky --save-dev which installed "husky": "^6.0.0" in my package.json.

Then I followed the tutorial and added the following object in package.json

 "husky": {
    "hooks": {
      "applypatch-msg": "echo \"[Husky] applypatch-msg\"",
      "pre-applypatch": "echo \"[Husky] pre-applypatch\"",
      "post-applypatch": "echo \"[Husky] post-applypatch\"",
      "pre-commit": "echo \"[Husky] pre-commit\""
    }
  }

As you can see, running git commit -m "some message!" should echo bunch of stuff which would mean that husky's pre-commit hook is working but instead nothing of the sort gets echoed. Now I have just no clue why is that not working. If it had worked I would have went on to add script in pre-commit hook to run my tests.

Here is the package.json file by the way:

{
  "name": "test app",
  "version": "1.0.1",
  "description": "test app",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "dev": nodemon index.js",
    "test": "find ./plugins -name '*test.js' | xargs mocha -R spec"
  },
  "dependencies": {
    "@hapi/boom": "^9.0.0",
    "@hapi/glue": "^7.0.0",
    "@hapi/good": "^9.0.0",
    "@hapi/good-console": "^9.0.0",
    "@hapi/good-squeeze": "^6.0.0",
    "@hapi/hapi": "^19.1.1",
    "@hapi/joi": "^17.1.1",
    "axios": "^0.19.2",
    "babel-eslint": "^10.1.0",
    "base-64": "0.1.0",
    "confit": "2.3.0",
    "hapi-auth-jwt2": "^8.8.1",
    "hapi-mongodb": "^9.0.0",
    "jws": "4.0.0",
    "mongodb": "^3.5.7",
    "pad-left": "2.1.0",
    "pino": "^5.16.0",
    "query-string": "^6.13.1",
    "redis": "^2.8.0",
    "selectn": "^1.1.2",
    "superagent": "3.8.2",
    "utf8": "^3.0.0",
    "uuid": "^3.4.0",
  },
  "devDependencies": {
    "chai": "^4.1.2",
    "chai-datetime": "^1.6.0",
    "chai-http": "^4.0.0",
    "husky": "^6.0.0",
    "mocha": "^5.1.1",
    "nodemon": "^2.0.4",
    "proxyquire": "^2.0.1",
    "sinon": "^5.0.10",
    "sinon-test": "^2.2.0"
  },
  "husky": {
    "hooks": {
      "applypatch-msg": "echo \"[Husky] applypatch-msg\"",
      "pre-applypatch": "echo \"[Husky] pre-applypatch\"",
      "post-applypatch": "echo \"[Husky] post-applypatch\"",
      "pre-commit": "echo \"[Husky] pre-commit\""
    }
  }
}
like image 865
gameFrenzy07 Avatar asked May 24 '26 06:05

gameFrenzy07


1 Answers

This is a bit old but maybe my solution will help someone.

It didn't work for me either to use this technique. The cleanest I found is

add script to package json:

"prepare": ""chmod ug+x ./installHooks.sh && ./installHooks.sh""

with installHooks.sh containing:

#!/bin/zsh
FILE=.husky
if test -d "$FILE"; then
  echo "$FILE folder already exists, deleting it before new husky installation";
  rm -rf "./$FILE"
  # else echo "$FILE doesn't exist yet";
fi
  cd ../ && husky install ./sub-project/.husky
  cd sub-project && npx husky add .husky/pre-commit 'cd sub-project && npm test'
  chmod ug+x ./.husky/*
  echo "hooks correctly installed. you may find them in `.husky/pre-commit`"
  echo "they will be triggered each time you commit"

Explanation: The prepare script will be ran automatically when running npm install or can be ran with npm run prepare It will change the mode of installHooks.sh to executable then run it. installHooks will check if the .husky folder already exists, delete it if it's the case, then install husky (note that going up one folder then down in sub-project is because my git repo contains several sub projects and I want to install this in the front-end one, here called sub-project. husky requires that you install it from the root so this is a workaround). Then adding pre-commit hook (npm test) Finally make husky hooks executable with chmod

Let me know if you have questions. I'm just starting writing scripts so this might not be the optimal way of doing this but it works

like image 77
Tsuina Avatar answered May 26 '26 19:05

Tsuina