Skip to content

Commit

Permalink
fix: defensive autolocation of node-gyp in make-spawn-args
Browse files Browse the repository at this point in the history
prevents runtime error in environments where `require.resolve` is not
available

Fall back to any existing `npm_config_node_gyp` provided by environment
  • Loading branch information
legobeat committed Oct 3, 2024
1 parent ee92273 commit c936a18
Showing 1 changed file with 13 additions and 2 deletions.
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') {
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

0 comments on commit c936a18

Please sign in to comment.