npm / cli

the package manager for JavaScript

Home Page:https://docs.npmjs.com/cli/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] INIT_CWD missing in v7

Geod24 opened this issue · comments

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.

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.

This was an oversight and is a bug in @npmcli/config. Will be fixed shortly.

@isaacs : I don't see any test in the above commit, so this could regress in the future.

@Geod24 The tests are in npm/config@7070885 This is a bug in @npmcli/config, not in the cli itself.

This issue still appears on my machine. I'm using npm@7.3.0. When I tried to install sharp, it does not run sharp's postinstall script to setup the bindings.

@KsRyY I've checked with 7.3.0 and the variable was set. If you're trying to debug note that the console output is hidden as of npm@7 so you may simply not see the familiar messages anymore, but the script is still being run in the background.

I also repro this with 7.3.0. My script is literally echo %INIT_CWD% (on Windows). It's not defined. None of the config settings from package.json are defined, either.

@KsRyY I've checked with 7.3.0 and the variable was set. If you're trying to debug note that the console output is hidden as of npm@7 so you may simply not see the familiar messages anymore, but the script is still being run in the background.

I probably need to open an new issue. My problem, specifically, is about lifecycle script not being run (could be related to this problem though). When I installed sharp on my project, the postinstall script must be run in order to download sharp's nodejs binding, but npm just simply extracts its wrapper code and not running the postinstall script, causing sharp to be not usable. I've tested npm with several other packages that has postinstall script (such as level) and none of the postinstall script is being run, so I'm sure it's not a package-specific problem.

It should be running them, but all success output is suppressed, so you’ll only see output if there’s a failure.