Cli: [BUG] INIT_CWD missing in v7

Created on 24 Oct 2020  路  6Comments  路  Source: npm/cli

Did a search on INIT_CWD, nothing relevant showed up. I also searched through the changelog and didn't find anything relevant.

Current Behavior:

In order to get the directory the user was in when the command was run, one can use process.env.INIT_CWD, as documented here.
This was introduced in v5.4.0 according to the changelog.

However, when upgrading from node 14 to node 15, INIT_CWD is not defined anymore. The upgrade hit me today, as I ran brew upgrade on my machine. I could reproduce on our Github CI.

Expected Behavior:

I did not expect INIT_CWD to disappear, or if it does, I expect a way to access similar information.

Steps To Reproduce:

I created a simple repository to see the regression: https://github.com/Geod24/npm-init-cwd-regression
The repository essentially consist of a package.json that does npm start, and index.js is as follow:

const process = require('process');
console.info(process.env);

I setup a Github action to test on Ubuntu with Node 10, 12, 14, and 15.
One can see, when looking at the environment, that the one in 15 is quite different.

Environment:

Can reproduce on Mac and Ubuntu, and other details are linked above.

Bug Needs Triage Release 7.x

Most helpful comment

I use INIT_CWD for building simple npm scripts to scaffold out new components.
In these scenarios, process.cwd() does not help me
because that will return the root of my project rather than the subdirectory I'm in.

This is a breaking change and annoying enough that I'm going to revert versions until it's fixed.

All 6 comments

Did a bit of research and found that the variable was set in a separate package called npm-lifecycle that has been removed from the npm CLI starting with version 7: INIT_CWD in npm-lifecycle.

What would INIT_CWD contain that wouldn't be in process.cwd()?

Usually these are the same. The only case I know of is in the case of a postinstall script being run. There process.cwd() (and env.PWD) is set to the package where the script runs but INIT_CWD still points to the initial package where the npm install was run.

I was able to detect whether a script runs in postinstall without the INIT_CWD variable and so am fine with it being deprecated. I don't know if there are any plugins that cannot be implemented without this variable.

I like that the console output for postinstall scripts is being hidden since version 7. Don't remember having seen anything useful there.

Seems like this can be closed?

Usually these are the same.

What ?

Scripts are run from the root of the module, regardless of what your current working directory is when you call npm run. If you want your script to use different behavior based on what subdirectory you're in, you can use the INIT_CWD environment variable, which holds the full path you were in when you ran npm run.

I use INIT_CWD for building simple npm scripts to scaffold out new components.
In these scenarios, process.cwd() does not help me
because that will return the root of my project rather than the subdirectory I'm in.

This is a breaking change and annoying enough that I'm going to revert versions until it's fixed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

admosity picture admosity  路  4Comments

Cohen-Carlisle picture Cohen-Carlisle  路  4Comments

jaydenseric picture jaydenseric  路  3Comments

1000i100 picture 1000i100  路  3Comments

ahuglajbclajep picture ahuglajbclajep  路  3Comments