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

未关闭
#10,344 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

@ultimate-kiran 已经在做这个了。

开始于 2026年11月25日。

  • #10351 来自 @ultimate-kiran —— 未关闭

评估

难度
4/5
预计耗时
1-2 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
领域
cli, devtools

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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
主要语言
JavaScript
星标
10.3k
派生
5k
平均合并
4 天 11 小时
30 天内合并 PR
26

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

npm/cli 的其他 Issue

查看 npm/cli 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。