Search code examples
githuskylint-staged

Husky pre-commit hook not running


I have a git directory "root" in which there are two folders: "frontend" and "backend". The .git folder is in "root". I am trying to setup husky and lint-staged in the "frontend" folder.

I have deliberately left some errors in my files in the "frontend" folder. However, when I stage the files and commit them, husky doesn't seem to run and the commit goes through.

npx lint-staged works fine to identify the errors. But the pre-commit hook doesn't seem to run.

I tried the troubleshooting mentioned here. They all check out apart from the 4th one: "Check that your version of Git is greater than 2.9". But isn't the latest Git version 2.42 (https://git-scm.com/)?

// root/fe/.lintstagedrc.json

{
  "**/*.{css,js,ts,jsx,tsx}": ["eslint", "prettier --write"]
}
# root/fe/.husky/pre-commit.sh

cd fe && npx lint-staged
// root/fe/package.json

{
  "name": "fe",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite --open",
    "build": "tsc && vite build",
    "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
    "prepare": "cd .. && husky install fe/.husky",
    // ...
  },
  "dependencies": {
    "react": "^18.2.0",
    // ...
  },
  "devDependencies": {
    "eslint": "^8.45.0",
    "eslint-plugin-prettier": "^5.0.1",
    "eslint-plugin-react": "^7.33.2",
    "husky": "^8.0.3",
    "lint-staged": "^15.0.2",
    "prettier": "^3.0.3",
    "typescript": "^5.0.2",
    "vite": "^4.4.5",
    // ...
  },
}

Solution

  • See the first step in the troubleshooting you linked:

    Ensure that you don't have a typo in your filename. For example, precommit or pre-commit.sh are invalid names.

    Your file is called pre-commit.sh. Rename it to pre-commit.