cli icon indicating copy to clipboard operation
cli copied to clipboard

[BUG] config var "`node-gyp`" not respected in npm 7

Open DeeDeeG opened this issue 4 years ago • 9 comments

Current Behavior:

Unlike in npm v6, the node-gyp config var is not respected.

(This config option sets a custom path to a working node-gyp install's bin/node-gyp.js file. When set, npm will attempt to use this copy of node-gyp if executing node-gyp to build native C++ code.)

Expected Behavior:

node-gyp at the custom path is used. (At the path set in the node-gyp config var.)

Steps To Reproduce:

  1. If you don't have one already, obtain a copy of node-gyp (an extra copy not bundled with npm) and install its dependencies: i. git clone https://github.com/nodejs/node-gyp ii. cd node-gyp && npm install
  2. Specify a node-gyp config var somehow. See the following options to do so: a) Do npm config set node-gyp=/path/to/node-gyp/bin/node-gyp.js b1) Do export npm_config_node_gyp=/path/to/node-gyp/bin/node-gyp.js (Linux/macOS) b2) Do Set npm_config_node_gyp=C:/path/to/node-gyp/bin/node-gyp.js (Windows) c) Use this flag with npm commands: --node-gyp=/path/to/node-gyp/bin/node-gyp.js
  3. In any project that depends on a module with native code... run npm rebuild --verbose --foreground-scripts

Note that the path of node-gyp is printed next to a cli label in the verbose output.

For example (cli output, click to expand):
gyp verb cli [
gyp verb cli   '/Users/[user]/n-prefix/bin/node',
gyp verb cli   '/Users/[user]/n-prefix/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js',
gyp verb cli   'rebuild'
gyp verb cli ]
  • With npm 6, the node-gyp config var is respected, and the node-gyp at the custom path is used.
  • With npm 7, the node-gyp config var is not respected. Regardless of the config var, node-gyp bundled with the running npm is used, OR if there is a copy of node-gyp as a top-level dependency in the project's node_modules folder, that copy from the local node_modules/node-gyp is used.

Environment:

  • OS: macOS 10.15.7
  • Node: 12.4.0
  • npm: 7.6.1

Related issue?

I think I tracked down the problem to the @npmcli/run-script module. See this issue I created at that repository for my best guess as to why this is happening: https://github.com/npm/run-script/issues/23

DeeDeeG avatar Mar 08 '21 18:03 DeeDeeG

I tested this by "poisoning" a copy of node-gyp so I'd be sure if it was used or not.

Simply put this at the beginning of the node-gyp/bin/node-gyp.js file:

throw new Error('hello from poisoned node-gyp')

I used to be able to specify what copy of node-gyp npm used with the node-gyp config var, but this stopped working in npm 7.


I didn't see any RFCs about this, so it's either an undocumented behavior change, or it's an oversight/bug.

I'd appreciate some clarity on whether it's meant to be deprecated or dropped functionality, in which case documenting that somewhere would be great, or if it's considered a bug, some pointers on how to get the bug fixed.

DeeDeeG avatar Mar 10 '21 16:03 DeeDeeG

I also find this issue, but I think it's node_gyp not node-gyp, am I right?

tavrez avatar Apr 25 '21 19:04 tavrez

@tavrez thanks for another person confirming this issue.

Both variable names (with underscore or dash) can be used interchangeably, at least in npm 6.

  • Env var npm_config_node_gyp or npm_config_node-gyp (this var name works in a case-insensitive way)
  • npm config var set in a .npmrc file, as npm config set node_gyp or npm config set node-gyp (also case-insensitive)
  • Command-line flag for npm:
    • npm rebuild --verbose --node_gyp=[path/to/node-gyp/bin/node-gyp.js] (flag --node-gyp must be all lowercase), or
    • npm rebuild --verbose --node-gyp=[path/to/node-gyp/bin/node-gyp.js] (flag --node_gyp also must be all lowercase)

DeeDeeG avatar Apr 25 '21 23:04 DeeDeeG

I have a CI test that uses this npm config var to test whether spaces in paths can cause an issue.

I would have to disable or re-write the test if we upgrade our infrastructure to use npm 7 instead of npm 6. Any thoughts on whether this might be fixed, or labelled "won'tfix", would be appreciated so I can know whether to re-write/delete the test, or just wait for the fix.

Thanks.

DeeDeeG avatar Apr 29 '21 18:04 DeeDeeG

This is a known issue; We'll try to resolve this when we upgrade to [email protected] in the very near future.

darcyclarke avatar May 07 '21 18:05 darcyclarke

I think this is an unintended consequence of https://github.com/npm/run-script/commit/e4c7cb33eba987e30637006178172e67801f9e92 ?

Might be the same underlying cause as what causes https://github.com/npm/cli/issues/3058.

DeeDeeG avatar Jul 02 '21 21:07 DeeDeeG

This is a known issue; We'll try to resolve this when we upgrade to [email protected] in the very near future.

Sorry for the bump, but any progress on this issue and also #3058? I can't use Visual Studio 2022 with this, it is supported in node-gyp v8.4.0 but it is not bundled even with the latest release of npm.

The only workaround for that is install node-gyp in the project locally as dev dependencies, but I can't find the reason why npm_config_node_gyp isn't respected in npm v7.x anymore

Yes I know this is related to npm/run-script#23

Hazmi35 avatar Nov 11 '21 17:11 Hazmi35

Any progress?

satoufuyuki avatar Dec 10 '21 13:12 satoufuyuki

Related:

  • https://github.com/npm/run-script/pull/221
  • https://github.com/npm/run-script/pull/222

legobeat avatar Sep 30 '24 01:09 legobeat

Re-adding this to npm means first making it a proper config item. That is being done here. @npmcli/run-script will also need to be made aware of this new config and only do its current behavior if it is not found.

wraithgar avatar Feb 24 '25 18:02 wraithgar

This issue is not fixed in latest Node.js 24(npm 11). After setting environment variable npm_config_node_gyp, it is still launching the bundled node-gyp when you run npm rebuild or npm install

rogerwang avatar May 07 '25 15:05 rogerwang