typicode / husky

Git hooks made easy 🐶 woof!
https://typicode.github.io/husky
MIT License
31.75k stars 999 forks source link

Husky failed to install with `Cannot read property 'toString' of null` message. #655

Closed panosangelopoulos closed 3 years ago

panosangelopoulos commented 4 years ago

OS: MacOS 10.15.2 (19C57) Git: git version 2.23.0

❯ node -v v12.14.0

"husky": "^4.0.6",

During the installation i'm getting the following error.

husky > Setting up git hooks 
Cannot read property 'toString' of null 
husky > Failed to install
typicode commented 4 years ago

Thanks for the report, could you try with HUSKY_DEBUG=1 and latest Husky version?

scottwhite commented 4 years ago

Had the same error. I run in a docker container (project files are on a shared volume). I have no git in the container, this is what I get with HUSKY_DEBUG=1

husky > Setting up git hooks husky:debug Current working directory is /opt/app/node_modules/husky Cannot read property 'toString' of null husky:debug TypeError: Cannot read property 'toString' of null at Object.checkGitVersion (/opt/app/node_modules/husky/lib/installer/checkGitVersion.js:12:32) at run (/opt/app/node_modules/husky/lib/installer/bin.js:60:31) at Object.<anonymous> (/opt/app/node_modules/husky/lib/installer/bin.js:88:1) at Module._compile (internal/modules/cjs/loader.js:956:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10) at Module.load (internal/modules/cjs/loader.js:812:32) at Function.Module._load (internal/modules/cjs/loader.js:724:14) at Module.require (internal/modules/cjs/loader.js:849:19) at require (internal/modules/cjs/helpers.js:74:18) at Object.<anonymous> (/opt/app/node_modules/husky/husky.js:20:3) husky > Failed to install

Installing git into the container fixed this issue.

th3oxen commented 4 years ago

I had the same issue with Windows 7 on local machine. The problem was that I did not have git registered as a PATH variable. Adding path\to\Git\bin solved the problem.

DavidRimar commented 3 years ago

Hi,

Could anyone please shed light on what husky is referring to when it says "Cannot read property 'toString' of null"? Would this be the pre-commit file in the .git/hooks folder?

I have the same issue, have tried troubleshooting this for a while.

OS: Windows 10 Git: git version 2.28.0 node -v v12.16.1

(1 Git is registered as a PATH variable (2 Have tried installing/uninstalling Husky (3 Have tried deleting .git/hooks/pre-commit file / making it executable

THE MAIN ISSUE: The pre-commit file is not being modified with Husky code! The installation message:

husky > Setting up git hooks Cannot read property 'toString' of null husky > Failed to install

Thanks for any help!

johnico commented 3 years ago

happened to me also. happens only during docker container build

> husky@4.3.0 install /opt/web/node_modules/husky
> node husky install

husky > Setting up git hooks
Cannot read property 'toString' of null
husky > Failed to install

tried to use version 2.6.0 and got this error

husky > Setting up git hooks
Can't find .git, skipping Git hooks installation.
Please check that you're in a cloned repository or run 'git init' to create an empty Git repository and reinstall husky.

the .git is in the dockerignore . i also tried to remove it from there

how can I fix it ?

hongbo-miao commented 3 years ago

I think the way install git will help. But husky requiring git when build feels not necessary. Add more info here in case it helps.

I just got same error after updating husky from 4.3.0 to 4.3.4. It happens both when I try to build in Heroku, and also in the docker.

error /app/node_modules/husky: Command failed.
Exit code: 1
Command: node husky install
Arguments:
Directory: /app/node_modules/husky
Output:
husky > Setting up git hooks
Cannot read property 'toString' of null
husky > Failed to install
theoludwig commented 3 years ago

Indeed, I also revert back to husky@4.3.0 while waiting for a fix.

rkrn commented 3 years ago

Also experienced this today. Reverting to husky@4.3.0 for now.

> node husky install
husky > Setting up git hooks
Cannot read property 'toString' of null
husky > Failed to install
theoludwig commented 3 years ago

Duplicate of #821 https://github.com/typicode/husky/issues/821#issuecomment-739430917

hongbo-miao commented 3 years ago

For me, I ended up with upgrading to Husky 5.0.4 by https://github.com/Hongbo-Miao/hongbomiao.com/pull/1420 And the Husky docs are well-written! These parts help:

jenskuhrjorgensen commented 3 years ago

Duplicate of #821 #821 (comment)

@divlo As far as I can see, the fix in the issue you linked is to install version 4.3.6. I'm already on that version and I'm still experiencing this issue.

jenskuhrjorgensen commented 3 years ago

Hi @typicode

This issue seems to have been around for quite some time, but has lately received some traction. Do you have a comment on how to fix this?

Best regards Jens

typicode commented 3 years ago

Hi @jenskuhrjorgensen,

Which version of npm are you using? If you're on npm 7, could you retry with the latest version?

jenskuhrjorgensen commented 3 years ago

Hi @typicode

I'm not on version 7 yet. I'm running 6.14.10.

It is worth noting, that Husky actually works as expected in my local environment, but it clutters my CI logs with the aforementioned error. The documentation says that Husky should not even be installed on CI by default, but it appears to try.

typicode commented 3 years ago

And what's the package manager version on the CI?

jenskuhrjorgensen commented 3 years ago

@typicode

It is the CI that is running npm v 6.14.10.

typicode commented 3 years ago

Could you retry with husky v4.3.8?

jenskuhrjorgensen commented 3 years ago

@typicode

With 4.3.8 I'm get ting git --version command failed. Got null instead.

typicode commented 3 years ago

@jenskuhrjorgensen Thanks for the detail.

Git version check happens before checking if husky is running in CI, that's why the error is displayed.

I feel It would be a bit complicated to improve that in v4 without changing install logic and maybe introducing unexpected bugs. Maybe if I have time later.

Also, besides git command not available in CI, I don't see why git --version fails.

As a workaround, you could prevent husky from installing (and this error message) by setting HUSKY_SKIP_INSTALL=1 environment variable.

jenskuhrjorgensen commented 3 years ago

Hi @typicode

It might happen because I don't have git installed in some of the Docker images that my CI uses. node:10-slim for instance. So if the git version check is part of husky, maybe do it after checking if husky should be installed at all (isCI).

typicode commented 3 years ago

Husky 6 is now MIT again. I'd recommend upgrading to fix this issue. Feel free to create a new one if needed :+1: