[BUG] config var "`node-gyp`" not respected in npm 7
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:
- If you don't have one already, obtain a copy of
node-gyp(an extra copy not bundled withnpm) and install its dependencies: i.git clone https://github.com/nodejs/node-gypii.cd node-gyp && npm install - Specify a
node-gypconfig var somehow. See the following options to do so: a) Donpm config set node-gyp=/path/to/node-gyp/bin/node-gyp.jsb1) Doexport npm_config_node_gyp=/path/to/node-gyp/bin/node-gyp.js(Linux/macOS) b2) DoSet 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 - 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-gypconfig var is respected, and thenode-gypat the custom path is used. - With npm 7, the
node-gypconfig var is not respected. Regardless of the config var,node-gypbundled with the runningnpmis used, OR if there is a copy ofnode-gypas a top-level dependency in the project'snode_modulesfolder, that copy from the localnode_modules/node-gypis 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
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.
I also find this issue, but I think it's node_gyp not node-gyp, am I right?
@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_gypornpm_config_node-gyp(this var name works in a case-insensitive way) - npm config var set in a
.npmrcfile, asnpm config set node_gypornpm 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-gypmust be all lowercase), ornpm rebuild --verbose --node-gyp=[path/to/node-gyp/bin/node-gyp.js](flag--node_gypalso must be all lowercase)
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.
This is a known issue; We'll try to resolve this when we upgrade to [email protected] in the very near future.
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.
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
Any progress?
Related:
- https://github.com/npm/run-script/pull/221
- https://github.com/npm/run-script/pull/222
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.
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