如何在windows中为npm run-scripts设置shell

use*_*444 61 windows shell npm

我在windows上运行npm并且想在运行脚本中使用&style并行操作但是在cmd中并行运行在我的package.json文件中有点乱 - 我想写 -

脚本:{"go":"cmd1&cmd2"}

但是npm执行cmd.exe下的脚本,该脚本不知道; 我可以将其更改为脚本:{"go":"bats/bat1.bat")其中bat1.bat是一个cmd bat文件,它使用windows样式调用或启动命令来并行运行命令.哪个有效,但给了我一个仅适用于Windows的脚本

如果我可以让npm在bash克隆或cygwin下运行脚本我会尝试配置:{"shell":"bash"}但是仍然运行cmd.exe会简单得多

有没有办法告诉npm使用特定的shell(而不是cmd.exe)运行脚本?

Dun*_*Kim 97

从npm 5.1开始

npm config set script-shell "C:\\Program Files (x86)\\git\\bin\\bash.exe"  
Run Code Online (Sandbox Code Playgroud)

(64位安装)

npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"
Run Code Online (Sandbox Code Playgroud)

请注意,您需要安装git for windows.

您可以通过运行来恢复它:

npm config delete script-shell
Run Code Online (Sandbox Code Playgroud)

  • `npm config set script-shell"C:\\ Program Files \\ Git \\ bin \\ bash.exe"`是我在Windows 10 64-Bit上运行并行脚本的所有问题的答案.@DuncanSungWKim감사합니다. (4认同)
  • 这应该是公认的答案!在具有Git Bash的Windows 10上运行(包括VS Code)。 (3认同)
  • 如果我已经在 git bash shell 中并使用此配置运行 `npm run <script`,则会生成一个新的 git bash windows 运行该脚本。这不是我想要的。有没有办法在调用 shell 中简单地调用 shell? (3认同)
  • @JHH - 我还从 vscode 打开了 shell,因为我使用的是 git-bash.exe 而不是 bash.exe。当我修复 vscode bash 与 script-shell bash 相同时,问题就解决了。 (3认同)
  • 如果您不确定 bash.exe 路径,您可以执行“npm config set script-shell bash”,因为 bash.exe 位置可能位于“PATH”中 (2认同)

Cra*_*sen 23

这是一种方法:

  1. 在项目bin目录中创建一个脚本,例如my_script.sh.
  2. 在package.json文件中,添加一行以使用bash运行脚本.例如:

    "scripts": {
      "boogie": "bash bin/my_script.sh"
    }
    
    Run Code Online (Sandbox Code Playgroud)

现在,您可以通过以下命令从npm运行bash脚本:

    npm run-script boogie
Run Code Online (Sandbox Code Playgroud)

不是很优雅,但它的工作原理.

如果您在Windows和Linux/Unix中进行开发,那么至少这种方法对两种环境都是相当便携的.

  • 另一种选择:"脚本":{"start":"node ./bin/www","bash":"bash -c","ls":"npm run bash ls"}` (3认同)

Jer*_*yal 16

您还可以对 npm 脚本使用跨平台 powershell https://github.com/powershell/powershell#get-powershell

要设置单个项目,请从项目根文件夹运行:

npm config set script-shell pwsh --userconfig ./.npmrc
Run Code Online (Sandbox Code Playgroud)

为所有节点项目进行全局设置:

npm config set script-shell pwsh [--global]
Run Code Online (Sandbox Code Playgroud)


Cra*_*sen 15

理想情况下,覆盖npm shell配置参数应该可以工作,但是在Windows中似乎npm(至少版本1.4.14)忽略该设置并改为使用cmd.exe.

在bash或Git Bash shell中使用以下命令来查找shell设置:

$ npm config ls -l | grep shell
Run Code Online (Sandbox Code Playgroud)

默认情况下,输出将是:

shell = "C:\\WINDOWS\\system32\\cmd.exe"
Run Code Online (Sandbox Code Playgroud)

但是,要覆盖默认的shell参数,可以将npmrc文件添加(或编辑)到\ Users\yourusername\AppData\Roaming \npm\etc目录.只需添加以下行:

shell = "C:\\Program Files (x86)\\git\\bin\\bash.exe"
Run Code Online (Sandbox Code Playgroud)

您使用的路径可以是bash.exe的任何有效路径.现在,如果运行上面的"npm config ls -l | grep shell"命令,您将看到以下输出,表明shell参数已被覆盖:

shell = "C:\\Program Files (x86)\\git\\bin\\bash.exe"
; shell = "C:\\WINDOWS\\system32\\cmd.exe" (overridden)
Run Code Online (Sandbox Code Playgroud)

也许有一天,新版本的npm会关注被覆盖的shell参数.

  • 这做同样的工作:`$ npm config set shell"C:\\ msys64 \\ usr \\ bin \\ bash"` (4认同)
  • 希望这有效。我更改了 COMSPEC 环境变量,它似乎是从该变量中提取的,但我无法让它正常工作。 (2认同)
  • 这是npm github相关主题https://github.com/npm/npm/issues/6543 (2认同)