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

fix: autolocation of node-gyp in make-spawn-args #221

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/make-spawn-args.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
/* eslint camelcase: "off" */
const setPATH = require('./set-path.js')
const { resolve } = require('path')
const npm_config_node_gyp = require.resolve('node-gyp/bin/node-gyp.js')
let npm_config_node_gyp
try {
/* istanbul ignore next */
if (typeof require === 'function' && typeof require.resolve === 'function') {
Copy link
Author

@legobeat legobeat Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The typeof require may seem redundant here considering we call require('path') just prior.

Rightfully or not, bundlers like webpack and esbuild may transform the direct require() calls but leave require.resolve intact. The extra check prevents runtime errors in such scenarios.

npm_config_node_gyp = require.resolve('node-gyp/bin/node-gyp.js')
}
} catch (er) {
/* istanbul ignore next */
}

const makeSpawnArgs = options => {
const {
Expand All @@ -23,8 +31,11 @@ const makeSpawnArgs = options => {
npm_package_json: resolve(path, 'package.json'),
npm_lifecycle_event: event,
npm_lifecycle_script: cmd,
npm_config_node_gyp,
})
/* istanbul ignore next */
if (typeof npm_config_node_gyp === 'string') {
spawnEnv.npm_config_node_gyp = npm_config_node_gyp
}

const spawnOpts = {
env: spawnEnv,
Expand Down