husky: Husky pre-commit fails with code 1 (error)
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"
}
}
}
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 40
- Comments: 59
Commits related to this issue
- chore(husky): fix pre-commit lastest version * Husky pre-commit fails with code 1 - (#1014): https://github.com/typicode/husky/issues/1014 — committed to focela/theme by trants 2 years ago
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-verifyfrom: here
try to restart your vscode
This worked for me
or
git config --unset core.hooksPathThis just disables husky for me.
This does NOT resolve the issue, but a work around to ignore the issue.
It looks like this is actually a
lint-stagedissue, specifically broken dependencies in version 11.2.4.See if you can reproduce this:
git commitfrom command line also fails:yarn lint-stagedis in fact silently failing and returning exit code 1:If so, the problem should be fixed soon: https://github.com/okonet/lint-staged/issues/1032
In the meantime, pinning
lint-stagedto11.1.4should resolve the problem for now.same with vscode for me
I hope you all know that by setting
git config --unset core.hooksPathyou 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/hooksfolder).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: https://github.com/okonet/lint-staged/issues/420
For me, this issue is not because of husky, but because of
lint-staged. I resolved it by removing lint-staged. I useeslint --cacheinsteadFor those who tried
git config --unset core.hooksPathwith a custom Husky setup in a root.huskydirectory, you’ll need to rungit config --local core.hooksPath .huskyto get Husky working again.I just renamed from
commitlint.config.jstocommitlint.config.cjsIt worked without any error.Me too, greate~
In my case “lint-staged” configuration was missing from
package.jsonafter a merge conflict was resolved. Added it back and no more issues.I came across same issue and I deleted my node_modules file than run yarn and it worked for me.
In my case, I had below line in my pre-commit file…
This returned an empty
FILES, so husky was giving the above error. With a little bit of digging I have changed the line to below, it worked fine. I don’t know why husky is exiting there and not just allowing the below line to work, that needs to be checked. But for those struggling like me here is the corrected version.FILES=$(git diff --cached --name-only | grep '^APIs/.*\.json' || true)This runs expectedly in each case…
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
.nvmrcfile.In my case when I checked I had version 14 pinned in my path.
I was able to work around this by:
nvm alias default <desired default, e.g. lts/hydrogen><.nvm_path>/alias/defaultinsteadgit config --unset core.hooksPathgit config --get core.hooksPathgit commit -m "type your comment here" --no-verifygit push origin yourbranchthese 4 lines of code are saving me
I realized that the right way to fix this error is not by bypassing it else your GitHub commit checks will fail when merging your branch. The best way to fix this issue is by simply making your commits the way husky understands it. For example:
git commit -m "feat: my commit message"This solution worked for me. I hope this helps.
randomly closed, it was the encoding was utf-16
maybe dont close without a comment
In my case I was running the perfectly fine pre-commit code
Which if, I ran separately as
.husky/pre-commitit worked (ofc afterchmod +x ./.husky/pre-commit).When I ran it via IntelliJ it kept failing with
husky - pre-commit script failed (code 1)Then I deleted_folder in.husky/_which I kept from when I first installed husky (npx husky init).Now the hook works as expected.
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" ] }Your
package.jsonmust be an actual JSON rather than just JavaScript Object. I change the following from:to
And the issue was fixed.
This worked for me:
First I ran
npx commitlintand I got a prompt to update commitlint. Apparently, it seemed that commitlint needed an updateAnd voila! Subsequent commits were successful
this worked for me:
create a file named “.lintstagedrc” with this inside
my current version of lint-staged is: 13.2.0
I checked mine and saw that my
lint-stagesconfigwas not directly in the root of the package.json
Maybe it will help someone, everything worked out for me after changing single quotes to double ones in package.json
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.
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
lint-stagednot supporting nodejs version 12 anymore. Update your node, it’ll workhttps://github.com/serverless/serverless/issues/11249#issuecomment-1186439595
It Worked for me 👍
I updated my VSCode version.This fixed my issue. Maybe restarting all VSCode window is the solution for this issue.
WORKED 😀
if deleting node modules and running lint in your project can’t help try to restart your computer
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.
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:
This fixed my issue. Just opened a new terminal in iTerm2
You must to verify this because with this command you’re disabling commit checks
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 lintbefore I commit. so after solving the error inyarn lint, mine commit works.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.