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

process.env.INIT_CWD is undefined!!! #640

Closed
HeavenSky opened this issue Jan 12, 2020 · 12 comments
Closed

process.env.INIT_CWD is undefined!!! #640

HeavenSky opened this issue Jan 12, 2020 · 12 comments

Comments

@HeavenSky
Copy link

HeavenSky commented Jan 12, 2020

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

@afc163
Copy link
Contributor

afc163 commented Jan 13, 2020

cnpm/cnpm#317

@williamthorsen
Copy link

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

@typicode
Copy link
Owner

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

@williamthorsen
Copy link

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

@RichiCoder1
Copy link

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

@euntak
Copy link

euntak commented Feb 21, 2020

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

@juannadin
Copy link

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



> husky@4.2.3 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

@naingaungphyo
Copy link

naingaungphyo commented Mar 19, 2020

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.

@dlebedynskyi
Copy link

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

@LordKenzo
Copy link

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

@akavoron
Copy link

akavoron commented Dec 6, 2020

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

@typicode
Copy link
Owner

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 👍

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