From c936a182cc2dda152a58c8c5ee59526e0762c86a Mon Sep 17 00:00:00 2001 From: legobt <6wbvkn0j@anonaddy.me> Date: Mon, 30 Sep 2024 01:35:05 +0000 Subject: [PATCH] fix: defensive autolocation of node-gyp in make-spawn-args prevents runtime error in environments where `require.resolve` is not available Fall back to any existing `npm_config_node_gyp` provided by environment --- lib/make-spawn-args.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/make-spawn-args.js b/lib/make-spawn-args.js index 8a32d71..8e3c8b6 100644 --- a/lib/make-spawn-args.js +++ b/lib/make-spawn-args.js @@ -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 { @@ -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,