process.env.INIT_CWD is undefined!!!
when i install husky v4.x.x, I found below error.
npm、git、node version is in below screenshot.

I saw the source code. Found:
process.env.INIT_CWD is undefined
// Get INIT_CWD env variable
function getInitCwdEnv() {
const { INIT_CWD } = process.env; // INIT_CWD is undefined !!!!
if (INIT_CWD === undefined) {
const { name, version } = which_pm_runs_1.default();
throw new Error(`INIT_CWD is not set, please upgrade your package manager (${name} ${version})`);
}
debug_1.debug(`INIT_CWD is set to ${INIT_CWD}`);
return INIT_CWD;
}
husky version is in below image:

https://github.com/cnpm/cnpm/issues/317
I have the same problem using Husky v4.2.1: The installer can't find INIT_CWD. It also can't find npm_config_user_agent. It fails when either of those values is missing.
A workaround is to supply them on the command line:
$ INIT_CWD="$PWD" npm_config_user_agent=$(yarn config get user-agent) \
node node_modules/husky/husky.js install
(This also works with npm config instead of yarn config.)
Steps to reproduce:
$ mkdir husky-bug && cd $_
$ npm init -y
$ npm install --save-dev husky
$ node node_modules/husky/husky.js install
/*
husky > Setting up git hooks
Cannot destructure property `name` of 'undefined' or 'null'.
husky > Failed to install
*/
INIT_CWD is provided during npm install or yarn. node_modules/husky/husky.js shouldn't be called directly.
If you want to re-run Husky install script, you can run npm rebuild or yarn rebuild (Yarn 2).
Thanks for the reply (and for the great module)!
INIT_CWDis provided duringnpm installoryarn.node_modules/husky/husky.jsshouldn't be called directly.
Yarn agrees with you: https://next.yarnpkg.com/advanced/migration#call-your-scripts-through-yarn-node-rather-than-node
It provides the yarn node command for that in v2. (Although that doesn't change what you say about not calling husky.js directly.)
If you want to re-run Husky install script, you can run
npm rebuildoryarn rebuild(Yarn 2).
It looks like there's no Yarn v1.x solution (other than falling back to npm rebuild).
This was also happening to me when running a normal npm ci fyi
We had the same issue. The node version was 12.4.0 and the npm was using 6.9.0.
Update the npm version to the latest.
In version npm 6.13.4, the husky is installed well with npm ci
While using versión husky 4.2.3, npm 6.14.2 and node 10.15.3, it fails at our continuous integration in jenkins. Not in development:
husky > Setting up git hooks
Cannot destructure property `name` of 'undefined' or 'null'.
husky > Failed to install
> [email protected] postinstall /app/node_modules/husky
> opencollective-postinstall || exit 0
Thank you for using husky!
If you rely on this package, please consider supporting our open collective:
> https://opencollective.com/husky/donate
npm ERR! Cannot read property 'length' of undefined
For me, I got this error.
opencollective-postinstall || exit 0 The token '||' is not a valid statement separator in this version.
- CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
- FullyQualifiedErrorId : InvalidEndOfLine
I opened about this issue here.
Edit: The above error is due to wrong npm scrip-shell setting as explained in the issue.
Run into this today. husky should not care on how it is being installed - npm, yarn, pnpm or upcoming new node package manager changes.
I've the same issue with yarn or npm. If I try to install manually I get:
husky > Setting up git hooks
husky:debug Current working directory is D:\dev\typescript\temp
Cannot destructure property 'name' of 'which_pm_runs_1.default(...)' as it is undefined.
husky:debug TypeError: Cannot destructure property 'name' of 'which_pm_runs_1.default(...)' as it is undefined.
my setup:
npm: 7.0.6 yarn: 1.22.10 git: 2.28.0.windows.1 SO: windows terminal: cmder
I had the same issue, node js v15.3.0, husky 5.0.4.
I think it's fixed with most recent versions of npm. Feel free to create a new issue if not or if I missed something :+1: