From 3790b7ae27c1dc5aae70517edd413fbe29b07f7a Mon Sep 17 00:00:00 2001 From: Shakti Date: Thu, 23 Jul 2026 15:33:47 +0530 Subject: [PATCH] fix: validate resolved npm path ends with .js before returning Some Node version managers (mise, asdf, proto) replace bin/npm with a non-JS wrapper script (e.g. a bash shim). fs.realpathSync succeeds on these (the file exists), but spawning 'node /path/to/bash-wrapper' fails with a SyntaxError. Validate the resolved path ends with '.js' before returning it; otherwise fall back to the conventional path. Fixes #7543. --- packages/cli/src/utils/installationInfo.ts | 28 ++++++++++++++-------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/cli/src/utils/installationInfo.ts b/packages/cli/src/utils/installationInfo.ts index f1c424b2a35..ae531662cc9 100644 --- a/packages/cli/src/utils/installationInfo.ts +++ b/packages/cli/src/utils/installationInfo.ts @@ -41,20 +41,28 @@ export function getNpmCliPath( // instead of throwing synchronously — getNpmCliPath is called from a // non-async site (handleAutoUpdate), and a returned best-effort path lets the // downstream spawn surface any failure through its 'error' handler. + // + // Some Node version managers (mise, asdf, proto) replace bin/npm with a + // non-JS wrapper script (e.g. a bash shim). fs.realpathSync succeeds on + // these (the file exists), but spawning `node /path/to/bash-wrapper` fails + // with a SyntaxError. Validate the resolved path ends with ".js" before + // returning it; otherwise fall back to the conventional path. const adjacentNpm = path.join(path.dirname(nodePath), 'npm'); try { - return fs.realpathSync(adjacentNpm); + const resolved = fs.realpathSync(adjacentNpm); + if (resolved.endsWith('.js')) return resolved; } catch { - return path.join( - path.dirname(nodePath), - '..', - 'lib', - 'node_modules', - 'npm', - 'bin', - 'npm-cli.js', - ); + // Fall through to the conventional path below. } + return path.join( + path.dirname(nodePath), + '..', + 'lib', + 'node_modules', + 'npm', + 'bin', + 'npm-cli.js', + ); } const debugLogger = createDebugLogger('INSTALLATION_INFO');