You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过npm/pnpm从package.json运行本地可执行文件?相对路径报错

解决npm/pnpm运行scripts中相对路径exe文件报错问题

问题场景

在package.json的scripts中配置相对路径执行本地exe文件:

"scripts": {
  "test": "./localPath/file.exe"
}

执行npm run test或pnpm test时触发错误:

'.' is not recognized as an internal or external command, operable program or batch file.

已验证的两种情况:

  • 直接在命令行执行./localPath/file.exe可正常运行
  • 将scripts中的路径改为绝对路径后,执行npm/pnpm命令能正常运行:
"scripts": {
  "test": "absolutePath/file.exe"
}

问题原因

Windows环境下,npm/pnpm默认使用cmd.exe执行scripts命令,而cmd.exe不支持类Unix风格的./相对路径写法,会把.当成独立命令解析,从而抛出错误。而直接在PowerShell或Git Bash中执行./路径时,这些终端能正确解析相对路径,所以可以正常运行。

解决方案

方案1:改用Windows风格相对路径

将路径中的斜杠替换为反斜杠,明确当前目录前缀:

"scripts": {
  "test": ".\\localPath\\file.exe"
}

方案2:指定用PowerShell执行命令

在scripts中直接调用PowerShell执行exe文件,PowerShell支持./路径写法:

"scripts": {
  "test": "powershell ./localPath/file.exe"
}

方案3:全局修改npm/pnpm的默认执行shell

执行以下命令将npm的默认脚本shell设置为PowerShell,后续所有scripts命令都会用PowerShell执行,无需修改路径:

npm config set script-shell "powershell.exe"

如果使用pnpm,同样修改配置:

pnpm config set script-shell "powershell.exe"

内容的提问来源于stack exchange,提问作者UniParse

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 14:35:34