Search code examples
gitpackage.jsonlint-stagedgit-husky

Unable to commit due to Husky failure in node application


I have created a node application and installed Husky7.0.4 and lint-staged12.3.5

following are the configuration for .husky/pre-commit --- file

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run pre-commit

and following are the package.json config

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon --exec babel-node index.js",
    "build": "rimraf dist/ && babel ./ --out-dir dist/ --ignore ./node_modules,./.babelrc,./package.json,./npm-debug.log --copy-files",
    "prettier:check": "prettier --check .",
    "prettier:fix": "prettier --write .",
    "lint:check": "eslint .",
    "lint:fix": "eslint --fix .",
    "pre-commit": "lint-staged",
    "prepare": "husky install"
  },
  "lint-staged": {
    "*.js": [
      "lint:check",
      "lint:fix",
      "prettier:fix"
    ]
  },
  "dependencies": {
    "lint-staged": "^12.3.5",
  },
  "devDependencies": {
    "@babel/cli": "^7.17.6",
    "@babel/core": "^7.17.5",
    "@babel/node": "^7.16.8",
    "@babel/preset-env": "^7.16.11",
    "eslint": "^8.10.0",
    "eslint-config-prettier": "^8.5.0",
    "husky": "^7.0.4",
    "nodemon": "^2.0.15",
    "prettier": "^2.5.1"
  } 

But I am getting the following error every time I am committing my code

> [email protected] pre-commit
> lint-staged

✔ Preparing lint-staged...
⚠ Running tasks for staged files...
  ❯ package.json — 1 file
    ❯ *.js — 1 file
      ✖ lint:check [ENOENT]
      ◼ lint:fix
      ◼ prettier:fix
↓ Skipped because of errors from tasks. [SKIPPED]
✔ Reverting to original state because of errors...
✔ Cleaning up temporary files...

✖ lint:check failed without output (ENOENT).
husky - pre-commit hook exited with code 1 (error)  

Solution

  • I am able to fix the issue by changing

    "lint-staged": {
        "*.{js,jsx,ts,tsx}": [
          "yarn lint:fix",
          "yarn prettier:check",
          
          "git add"
        ]
      },