husky icon indicating copy to clipboard operation
husky copied to clipboard

process.env.INIT_CWD is undefined!!!

Open HeavenSky opened this issue 6 years ago • 12 comments

when i install husky v4.x.x, I found below error.

npm、git、node version is in below screenshot. image

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: image

HeavenSky avatar Jan 12 '20 14:01 HeavenSky

https://github.com/cnpm/cnpm/issues/317

afc163 avatar Jan 13 '20 02:01 afc163

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
*/

williamthorsen avatar Jan 27 '20 18:01 williamthorsen

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).

typicode avatar Feb 15 '20 01:02 typicode

Thanks for the reply (and for the great module)!

INIT_CWD is provided during npm install or yarn. node_modules/husky/husky.js shouldn'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 rebuild or yarn rebuild (Yarn 2).

It looks like there's no Yarn v1.x solution (other than falling back to npm rebuild).

williamthorsen avatar Feb 15 '20 22:02 williamthorsen

This was also happening to me when running a normal npm ci fyi

RichiCoder1 avatar Feb 17 '20 23:02 RichiCoder1

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

euntak avatar Feb 21 '20 09:02 euntak

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

juannadin avatar Mar 05 '20 13:03 juannadin

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.

naingaungphyo avatar Mar 19 '20 02:03 naingaungphyo

Run into this today. husky should not care on how it is being installed - npm, yarn, pnpm or upcoming new node package manager changes.

dlebedynskyi avatar Oct 26 '20 22:10 dlebedynskyi

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

LordKenzo avatar Nov 09 '20 18:11 LordKenzo

I had the same issue, node js v15.3.0, husky 5.0.4.

akavoron avatar Dec 06 '20 00:12 akavoron

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:

typicode avatar Jan 20 '21 15:01 typicode