[BUG] npx on Windows: bins with pnpm .cmd shims fail with MODULE_NOT_FOUND since #10044 quotes the executable name

Aperta
#10,344 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@ultimate-kiran ci sta già lavorando.

Dal 25/11/2026.

  • #10351 di @ultimate-kiran — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
1-2 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
javascript, node.js
Ambito
cli, devtools

Direzione di ricerca

The cause is in libnpmexec/lib/run-script.js, where #10044 began quoting the executable name for cmd.exe. Reproduce on Windows by running npx against a bin whose shim uses %~dp0 directly, and compare the quoted and unquoted forms. A fix already has an open pull request (#10351), so review or test that work rather than starting a parallel change.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Triage Requested
Is there an existing issue for this?
  • I have searched the existing issues
This issue exists in the latest npm version
  • I am using the latest npm
Current Behavior

On Windows, npx <bin> fails with MODULE_NOT_FOUND when the bin's .cmd shim in node_modules\.bin was written by pnpm. The shim looks for the package one folder too high:

PS C:\project\pkg> npx wrangler --version
npm notice run ^"wrangler^" --version
Error: Cannot find module 'C:\project\wrangler\bin\wrangler.js'
  code: 'MODULE_NOT_FOUND'

The file is really at C:\project\pkg\node_modules\wrangler\bin\wrangler.js. This worked before the upgrade to npm 12.

Cause, as far as I can tell: #10044 made libnpmexec/lib/run-script.js quote the executable name for cmd.exe, so the script becomes "wrangler" --version. When cmd.exe starts a batch file by a quoted bare name found through PATH, and arguments follow, %~dp0 inside that batch file resolves to the current directory instead of the batch file's own folder. pnpm's shim runs node "%~dp0\..\wrangler\bin\wrangler.js", so it points at C:\project\pkg\..\wrangler\....

npm's own cmd-shim output is not affected because it reads %~dp0 through CALL :find_dp0. Shims written by other tools (pnpm, and anything else that uses %~dp0 directly) break.

The same quirk without npm, run from PowerShell 7 in C:\tmp\dp0, with C:\tmp\dp0\bin\showdir.cmd containing @echo %~dp0 and C:\tmp\dp0\bin on PATH:

cmd /d /s /c 'showdir a b'      -> C:\tmp\dp0\bin\   (correct)
cmd /d /s /c '"showdir" a b'    -> C:\tmp\dp0\       (current directory)
Expected Behavior

npx wrangler --version runs the bin from node_modules\.bin, as it did before npm 12.

Possible fixes: only quote the name when it contains cmd metacharacters, or resolve the bin to its absolute path before quoting it.

Steps To Reproduce
  1. On Windows, in a project installed with pnpm: pnpm add -D wrangler (any package with a bin should do).
  2. From PowerShell or cmd, run npx wrangler --version with npm 12.2.3.
  3. See Cannot find module '<parent of project>\wrangler\bin\wrangler.js'.

pnpm exec wrangler --version works in the same folder. Workaround: setting npm_config_script_shell=pwsh makes npx use the .ps1 shim, which resolves its folder correctly.

Environment
  • npm: 12.2.3
  • Node.js: 24.15.0
  • OS Name: Windows 11 Pro (10.0.26200)
  • System Model Name: x64 desktop
  • npm config: defaults (no script-shell set)
  • Shims generated by pnpm 11.2.2
Lingua principale
JavaScript
Stelle
10.3k
Fork
5k
Merge medio
4g 11h
PR unite (30g)
26

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di npm/cli

Tutte le issue di npm/cli

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.