husky: pre-commit hook failed (add --no-verify to bypass)

I’m getting the following error:

Commit failed with error
			0 file committed, 2 files failed to commit: Add reformat with prettier and eslint precommit
			husky > pre-commit (node v13.9.0)
			C:\Users\...\frontend\node_modules\.bin/../node/bin/node: line 1: This: command not found
			husky > pre-commit hook failed (add --no-verify to bypass)

This is my package.json

  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "*.{js,jsx,ts,tsx}": [
      "prettier --write",
      "eslint --fix",
      "git add"
    ],
    "*.{html,css,less,ejs,json}": [
      "prettier --write",
      "git add"
    ]
  }

I tried everything that is mentioned here but nothing works.

I have the latest vesion of everything: intellij IDEA 2020.1, node 13.13.0, npm 6,14,4, git 2.24.0.windows.2, husky 4.2.5

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 12
  • Comments: 21

Most upvoted comments

Finally found a solution. The issue (even though it’s not an issue! ) is because of the hooks created by react. I simply deleted the hooks folder for git which defines the pre-commit hooks and hence can push after that.

Edit: You can also skip hooks when you provide the git command line argument —no-verify, git push origin master --no-verify, or use Sourcetree‘s Bypass commit hooks setting (in the menu to the top right of the commit message field)

Hey @cjoecker, I believe this issue is because of the fact that git has its own pre-hooks, and now, with husky, you have added yours. I think there is a conflict between those two.

For a temporary solution, you can just delete the hook folder from ./git folder from your project or maybe move it out of your project folder(if you want). This worked for me. Hope so for you too.

I updated husky to the latest version (^5.0.9) and the problem was gone.

same here, with husky 4.2.5,eslint 7.0.0, prettier 2.0.5,pre-commit doesn’t work.

Running husky-run lint-staged we can face an error lint-staged: command not found ... code 1:

husky > pre-commit (node v14.5.0)
sh: lint-staged: command not found
husky > pre-commit hook failed (add --no-verify to bypass)
npm ERR! code 1
npm ERR! path ~/Development/project
npm ERR! command failed
npm ERR! command sh -c husky-run "pre-commit" ""

npm ERR! A complete log of this run can be found in:
npm ERR!     ~/.npm/_logs/2021-03-20T10_11_56_707Z-debug.log

…from following line:

node_modules/husky/lib/runner/index.js:48

    if (status !== 0) {
        const noVerifyMessage = [
            'commit-msg',
            'pre-commit', /* <-- we are here */
            'pre-rebase',
            'pre-push',
        ].includes(hookName)
            ? '(add --no-verify to bypass)' /* <-- we are here */
            : '(cannot be bypassed with --no-verify due to Git specs)';
        console.log(`husky > ${hookName} hook failed ${noVerifyMessage}`);
    }
    // If shell exits with 127 it means that some command was not found.
    // However, if husky has been deleted from node_modules, it'll be a 127 too.
    // To be able to distinguish between both cases, 127 is changed to 1.
    if (status === 127) {
        return 1;
    }

So, this means, that lint-staged command is not found in node_modules/.bin/lint-staged -> ../lint-staged/bin/lint-staged.js

You just forgot to install it! (But it works running npx lint-staged)

The REAL solution is:

npm install --save-dev lint-staged

Finally found a solution. The issue (even though it’s not an issue! ) is because of the hooks created by react. I simply deleted the hooks folder for git which defines the pre-commit hooks and hence can push after that.

Edit: You can also skip hooks when you provide the git command line argument —no-verify, git push origin master --no-verify, or use Sourcetree‘s Bypass commit hooks setting (in the menu to the top right of the commit message field)

Not really a solution if you want the hooks to run. If you just delete the folder then it just stops working. I am having the same issue. Only happens on my Windows machine and I still haven’t found a solution.

When I try to make a commit I get this:

...>git commit -m "test"

husky > pre-commit (node v14.15.1)
C:\Program Files\nodejs/node_modules/node/bin/node: line 1: This: command not found
husky > pre-commit hook failed (add --no-verify to bypass)

package.json

  "devDependencies": {
    ...
    "husky": "^4.3.0",
    "lint-staged": "^10.5.1",
    ...
  }

.huskyrc.json

{
  "hooks": {
    "pre-commit": "lint-staged"
  }
}

.lintstagedrc.json

{
  "*.+(js|jsx)": ["eslint"]
}

On Mac and Linux this issue does not seem to happen. I tired uninstalling nodejs and installing the latest version, but it did not help.

Edit:

Managed to make it work by changing

.huskyrc.json to

{
  "hooks": {
    "pre-commit": "npm run lint"
  }
}

However now I am just bypassing lint-staged, but what if I want that to work too?

Same on Windows:

husky > pre-commit (node v11.15.0)
<CENSORED>\node_modules\.bin/../node/bin/node: line 1: This: command not found

Why is it using node v11, when I have another node version installed?

node --version
v12.16.3