Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Husky pre-commit fails with code 1 (error) #1014

Closed
abhinavkgrd opened this issue Jul 21, 2021 · 54 comments
Closed

Husky pre-commit fails with code 1 (error) #1014

abhinavkgrd opened this issue Jul 21, 2021 · 54 comments

Comments

@abhinavkgrd
Copy link

abhinavkgrd commented Jul 21, 2021

Committing from vs-code Source control returns this error but the command line works fine

vscode version - 1.58.2

Command output

husky - pre-commit hook exited with code 1 (error)

pre-commit

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

yarn lint-staged

package json

{
"scripts": {
    "lint-staged": "lint-staged",
    "postinstall": "husky install"
  },
"devDependencies":{
    "husky": "^7.0.0",
    "lint-staged": "^11.0.0",
}

 "lint-staged": {
    "src/**/*.{js,jsx,ts,tsx}": "eslint"
  },

"husky": {
    "hooks": {
      "pre-commit": "yarn run lint-staged"
    }
  }
}

@zackseuberling
Copy link

Similar issues with VS Code Version: 1.59.0

@kapilrc
Copy link

kapilrc commented Sep 29, 2021

Having same issue with VS code version 1.60.2

@aaronadamsCA
Copy link

It looks like this is actually a lint-staged issue, specifically broken dependencies in version 11.2.4.

See if you can reproduce this:

  • git commit from command line also fails:

    $ git commit --allow-empty-message
    husky - pre-commit hook exited with code 1 (error)
  • yarn lint-staged is in fact silently failing and returning exit code 1:

    $ yarn lint-staged
    $ echo $?
    1

If so, the problem should be fixed soon: lint-staged/lint-staged#1032

In the meantime, pinning lint-staged to 11.1.4 should resolve the problem for now.

@zerzavot
Copy link

Husky can prevent you from bad git commit, git push and more. If you are getting this error check your code syntax, in case if you are getting this error even your code is valid. Please use the below command.

git commit -m "message" --no-verify

from: here

@ooliver1
Copy link

ooliver1 commented Nov 21, 2021

same with vscode for me

> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file - -S
C:\...\commitlint.config.js:1
��m


SyntaxError: Invalid or unexpected token
    at wrapSafe (internal/modules/cjs/loader.js:988:16)
    at Module._compile (internal/modules/cjs/loader.js:1036:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Module.require (internal/modules/cjs/loader.js:961:19)
    at module.exports (C:\Users\olive\node_modules\import-fresh\index.js:32:59)
    at loadJs (C:\Users\olive\node_modules\cosmiconfig\dist\loaders.js:16:18)
    at Explorer.loadFileContent (C:\Users\olive\node_modules\cosmiconfig\dist\Explorer.js:84:32)
    at Explorer.createCosmiconfigResult (C:\Users\olive\node_modules\cosmiconfig\dist\Explorer.js:89:36)
husky - commit-msg hook exited with code 1 (error)
// commitlint.config.js
module.exports = {
    extends: ["@commitlint/config-conventional"]
};

@ooliver1
Copy link

randomly closed, it was the encoding was utf-16

maybe dont close without a comment

@sally2015
Copy link

try to restart your vscode

@zerzavot
Copy link

Husky can prevent you from bad git commit, git push and more. If you are getting this error check your code syntax, in case if you are getting this error even your code is valid. Please use the below command.

git commit -m "message" --no-verify

from: here

or git config --unset core.hooksPath

@TheExGenesis
Copy link

TheExGenesis commented Jan 6, 2022

yarn lint-staged and git commit work fine, the only occasion in which it doesn't work is when I commit from VS Code Source Control (VSC's GUI for git commands). After editing ./.huskyrc the output console reads

> git ls-files --stage -- /home/genesis/Documents/unigraph/unigraph-dev/.huskyrc
> git cat-file -s fdac8cc646628741017ad104f503ebc3507a6e29
> git show --textconv :.huskyrc
Unexpected identifier

@chicken-suop
Copy link

chicken-suop commented Feb 7, 2022

For me, this issue is not because of husky, but because of lint-staged. I resolved it by removing lint-staged. I use eslint --cache instead

@annielmenezes
Copy link

Hi people, a have a similar issue to run commands in pre-push hook. Via command-line it works without problems.
I'm using nvm with a specific version to the project path.

My VS Code version is:

Version: 1.64.0
Commit: 5554b12acf27056905806867f251c859323ff7e9
Date: 2022-02-03T04:20:17.224Z
Electron: 13.5.2
Chromium: 91.0.4472.164
Node.js: 14.16.0
V8: 9.1.269.39-electron.0
OS: Darwin x64 20.6.0

Output error:
.husky/pre-push: line 4: npm: command not found

Inside file I have a npm command:
npm run test:ci

@bivainis
Copy link

In my case "lint-staged" configuration was missing from package.json after a merge conflict was resolved. Added it back and no more issues.

@prapurnakumariTR
Copy link

git config --unset core.hooksPath

This worked for me

@Liiva
Copy link

Liiva commented May 13, 2022

In my case when trying to commit from my IDE it would give the non-descriptive error Unexpected identifier, while committing from command line worked fine.

It turned out my IDE used an earlier node version (6.14.4 vs 7.19.1 from my terminal), upping the version fixed this for me.

@ooliver1
Copy link

it seems from this thread that husky can have a lot of issues that cause the same vague error, this might need to be addressed

@XuXiaowen
Copy link

git config --unset core.hooksPath

这对我有用

git config --unset core.hooksPath

This worked for me

Me too, greate~

@magnus-bakke
Copy link

git config --unset core.hooksPath

This worked for me

This just disables husky for me.

@sh977218
Copy link

Husky can prevent you from bad git commit, git push and more. If you are getting this error check your code syntax, in case if you are getting this error even your code is valid. Please use the below command.
git commit -m "message" --no-verify
from: here

or git config --unset core.hooksPath

This does NOT resolve the issue, but a work around to ignore the issue.

@sowiecki
Copy link

I was able to resolve this error by running each precommit command individually. Turns out my test command (jest) was causing the error due to an obsolete snapshot.

Note that Jest seems to hide that this actually causes an error code 🤦 and instead makes it look like a warning, so if this is your problem you'll never actually see an error code directly from Jest. That's probably why this only appeared as an error through Husky for me, and I imagine there could be many other situations where an individual command is swallowing the error code to make it look like Husky's fault.

@aghwotu
Copy link

aghwotu commented Aug 17, 2022

I was able to resolve this error by running each precommit command individually. Turns out my test command (jest) was causing the error due to an obsolete snapshot.

Note that Jest seems to hide that this actually causes an error code 🤦 and instead makes it look like a warning, so if this is your problem you'll never actually see an error code directly from Jest. That's probably why this only appeared as an error through Husky for me, and I imagine there could be many other situations where an individual command is swallowing the error code to make it look like Husky's fault.

This was my issue. Thank you for your comment.

@jeyhunhasanov
Copy link

I just renamed from commitlint.config.js to commitlint.config.cjs
It worked without any error.

@yyc13236
Copy link

Got the same error.
My issue might be that hooks that should be run before committing or pushing have an error.
eg., in my .husky/pre-commit
it will run yarn lint before I commit.
so after solving the error in yarn lint, mine commit works.

@Tsekhmister
Copy link

Maybe it will help someone, everything worked out for me after changing single quotes to double ones in package.json
image

@allygraham
Copy link

I got the same error as well.
Turns out I was running Node v10 when trying to commit, a quick swap over to v16 and all was good.

@fmruiz
Copy link

fmruiz commented Jan 16, 2023

git config --unset core.hooksPath

This worked for me

You must to verify this because with this command you're disabling commit checks

@god-of-js
Copy link

I checked mine and saw that my lint-stages config


  "lint-staged": {
    "**/*.{js,jsx,ts,tsx}": [
      "npm run lint"
    ]
  }

was not directly in the root of the package.json

@mushyoom
Copy link

mushyoom commented Feb 8, 2023

git config --unset core.hooksPath

This worked for me

Thanks, you saved my time.

@andrii-lundiak
Copy link

andrii-lundiak commented Mar 4, 2023

I hope you all know that by setting git config --unset core.hooksPath you basically DISABLE Husky at all from codebase. So it's NOT a fix for Husky, it's a break of Husky (and giving control of hooks back to regular Git setup ./git/hooks folder).

UPD. What actually helped me, is a silly reason - VS Code required actual restart, and OF ALL instances!!!
I use multiple workspaces, so one VS Code instance restart was NOT enough. Ideally Restart PC for 200% proof.

PS. Look my last 2 comments with details troubleshooting notes here:
lint-staged/lint-staged#420

@mort3za
Copy link

mort3za commented Mar 7, 2023

VS Code required actual restart, and OF ALL instances!!!

This fixed my issue. Just opened a new terminal in iTerm2

@ramBlanco
Copy link

this worked for me:

create a file named ".lintstagedrc" with this inside

{
  "*.{ts,tsx}": ["eslint --fix", "prettier --write"]
}

my current version of lint-staged is: 13.2.0

@latanoel
Copy link

This worked for me:

First I ran npx commitlint and I got a prompt to update commitlint. Apparently, it seemed that commitlint needed an update

Need to install the following packages:
  commitlint@17.6.1
Ok to proceed? (y) y

And voila! Subsequent commits were successful

@bravo-stack
Copy link

Husky can prevent you from bad git commit, git push and more. If you are getting this error check your code syntax, in case if you are getting this error even your code is valid. Please use the below command.
git commit -m "message" --no-verify
from: here

or git config --unset core.hooksPath

The git config --unset core.hooksPath worked for me.

@agustingorgni
Copy link

Just create a file called .lintstagedrc and put this:

{
  "*.{js,jsx}": ["npm run lint:js"]
}

@SiegeSailor
Copy link

Your package.json must be an actual JSON rather than just JavaScript Object. I change the following from:

  "lint-staged": {
    "**/*": ["prettier --write --ignore-unknown"],
    "**/*.{ts,tsx}": ["jest --bail"]
  },

to

  "lint-staged": {
    "**/*": "prettier --write --ignore-unknown",
    "**/*.{ts,tsx}": "jest --bail"
  },

And the issue was fixed.

@kojikojiXX
Copy link

That error happened me, too.
I add following line to my package.json, then that error was fixed.

"lint-staged": { "src/**/*.{js,jsx,ts,tsx,json,css,scss,md}": [ "prettier --write" ] }

@DeadLoss1801
Copy link

{
"test" : "mocha"
}

@aldoprogrammer
Copy link

git config --unset core.hooksPath
git config --get core.hooksPath
git commit -m "type your comment here" --no-verify
git push origin yourbranch

these 4 lines of code are saving me

@jcubic
Copy link

jcubic commented Aug 7, 2023

I have the same error without any output:

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

FILES=`git diff --name-only --cached --diff-filter=d | grep -E '(ts|tsx)$'`;

if [ ! -z "$FILES" ]; then
  for file in $FILES; do
      npx prettier --write $file;
      npx eslint --fix $file;
  done
  git add $FILES
else
  true
fi

Is there any way to force Husky to show what is the error? My script works fine If I comment out . "$(dirname -- "$0")/_/husky.sh" and return true.

I've also tried this:

FILES=`git diff --name-only --cached --diff-filter=d | grep -E '(ts|tsx)$'`;

for file in $FILES; do
    npx prettier --write $file;
    npx eslint --fix $file;
done
test ! -z "$FILES" && git add $FILES || true

and Husky keep showing an error.

@jcubic
Copy link

jcubic commented Aug 7, 2023

So the problem was if the FILES is empty grep will return non-zero code and the whole hook will fail and the rest will not be executed.

I've solved the issue with this code:

FILES=`git diff --name-only --cached --diff-filter=d | grep -E '(ts|tsx)$' || true`;

if [ ! -z "$FILES" ]; then
  for file in $FILES; do
      npx prettier --write $file;
      npx eslint --fix $file;
  done
  git add $FILES
fi

@superbland
Copy link

I was having this problem and the output was complaining about "Unexpected token '??='". I'm using nvm, and this seems to be related to which version of node is loaded by default in the terminal (in this case, one which didn't understand this operator), regardless of my config switching node version based on the presence of a .nvmrc file.

In my case when I checked I had version 14 pinned in my path.

I was able to work around this by:

  1. Setting nvm alias default <desired default, e.g. lts/hydrogen>
  2. Removing version 14 from my path, and adding <.nvm_path>/alias/default instead

@fnavarrodev
Copy link

In my case husky was always getting node 14. Changing nvm default version didn't work or changing the PATH variable because the version was pinned in my .huskyrc file. I recommend you all checking that evil file.

@mjaynixxz
Copy link

In my case I keep getting this error, I really dunno where the issue is coming from, there is not a clear message to indicate the problem, husky is installed properly, if I am to comment out the script related to npm or npx, it just works fine. But the npm n npx commands keeps getting canceled

image

@jcubic
Copy link

jcubic commented Sep 13, 2023

@mjaynixxz this is something different because you have an error from npm, not only the error code. Did you look into the log file?

@usermilena
Copy link

if deleting node modules and running lint in your project can't help try to restart your computer

@Haseeb78692
Copy link

Husky can prevent you from bad git commit, git push and more. If you are getting this error check your code syntax, in case if you are getting this error even your code is valid. Please use the below command.
git commit -m "message" --no-verify
from: here

or git config --unset core.hooksPath

WORKED 😀

@NakZMichael
Copy link

I updated my VSCode version.This fixed my issue.
Maybe restarting all VSCode window is the solution for this issue.

@amanr-dev
Copy link

Husky can prevent you from bad git commit, git push and more. If you are getting this error check your code syntax, in case if you are getting this error even your code is valid. Please use the below command.

git commit -m "message" --no-verify

from: here

It Worked for me 👍

@OahMada
Copy link

OahMada commented Jan 9, 2024

In my case, everything works if I commit from VSCode's integrated terminal, but it's not the case in SourceTree.

@ktsangop
Copy link

ktsangop commented Feb 2, 2024

I was having this problem and the output was complaining about "Unexpected token '??='". I'm using nvm, and this seems to be related to which version of node is loaded by default in the terminal (in this case, one which didn't understand this operator), regardless of my config switching node version based on the presence of a .nvmrc file.

In my case when I checked I had version 14 pinned in my path.

I was able to work around this by:

  1. Setting nvm alias default <desired default, e.g. lts/hydrogen>
  2. Removing version 14 from my path, and adding <.nvm_path>/alias/default instead

Thanks for this

Just adding that I had nvm installed inside git bash (for Windows), and I wasn't able to find the path to the nvm installed nodejs instances.
So I removed my system's default nodejs installation (which was an older one), and installed a more recent one.
That way, husky was able to point to a compatible nodejs version, and my CLI (git bash) kept pointing to the nvm defined "default" version.

@carolisc
Copy link

I had this problem today with "lint-staged": "^15.2.2"

Tried many things from this thread and another, but nothing worked so I decided to go with git commit -m "message" --no-verify

P.S. if anyone would like to know: "husky": "^8.0.0", "prettier": "^3.2.5", "vite": "^5.2.0"

@kamwithak
Copy link

git config --unset core.hooksPath

This works but you are now missing your hooks 😆

@JJozef
Copy link

JJozef commented Apr 20, 2024

Same error

image

"husky": "9.0.11",
"@commitlint/cli": "19.2.2",
"@commitlint/config-conventional": "19.2.2",
"lint-staged": "15.2.2",

@veeramani-arthanari
Copy link

lint-staged not supporting nodejs version 12 anymore. Update your node, it'll work

serverless/serverless#11249 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests