【问题标题】:How to set shell for npm run-scripts in Windows如何在 Windows 中为 npm 运行脚本设置 shell
【发布时间】:2014-06-08 05:31:48
【问题描述】:

我在 Windows 上运行 npm,并希望在运行脚本中使用 & 样式并行操作 但是在cmd中并行运行有点混乱 在我的 package.json 文件中我想写 -

scripts: { "go": "cmd1 & cmd2"} 

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

如果我可以让 npm 在 bash 克隆或 cygwin 下运行脚本会简单得多。

我试过了 config: { "shell": "bash"} 但仍然运行 cmd.exe

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

【问题讨论】:

  • 是否 [仍然] 无法在 package.json 中指定 shell?我将 npm 用于 windows 和 bash 脚本,并且无法覆盖特定 package.json 的默认“shell”或“script-shell”设置?

标签: windows shell npm


【解决方案1】:

您还可以将跨平台的 powershell https://github.com/powershell/powershell#get-powershell 用于 npm 脚本。

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

npm config set script-shell pwsh --userconfig ./.npmrc

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

npm config set script-shell pwsh [--global]

【讨论】:

    【解决方案2】:

    就我而言,我只需要从 Bash 内部运行 npm start。我运行cmd,然后通过运行"c:\Program Files\Git\bin\bash.exe" 打开bash。在 bash shell 下,我可以成功调用 npm buildnpm start

    如果您正在使用 Git,您可能已经拥有 bash。如果没有,你可以install它。

    希望这可以节省一些人的时间。

    【讨论】:

    • 不适用于所有在 linux 上运行的脚本,例如 PORT=8000
    【解决方案3】:

    从 npm 5.1 开始

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

    (64位安装)

    npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"
    

    请注意,您需要有git for windows installed

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

    npm config delete script-shell
    

    【讨论】:

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

    只用CMD的方式运行.bat

    .json
    "scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test --env=jsdom",
        "eject": "react-scripts eject",
        "app": "cd build & browser-sync start --server --files 'index.html'",
        "bat": "start start-browser.bat",
        "starts": "start http://localhost:7777/datas/ && start http://localhost:7777/Info/"
    },
    
    。蝙蝠
    start http://localhost:7777/datas/ && start http://localhost:7777/Info/
    

    【讨论】:

      【解决方案5】:

      为此目的使用专门创建的 node_module。我建议使用npm-run-all,但是others是存在的,比如parallelshell

      Parallelshell 示例如下,可直接替换您的问题。

      "scripts": {
          "parallelexample1": "parallelshell \"echo 1\" \"echo 2\" \"echo 3\""
      },
      

      以下命令:

      npm run parallelexample1
      

      适用于 windows 和 unix(Linux/MacOS)。

      有趣的是 npm-run-all 不支持 shell 命令;因此,我们需要将所有 shell 命令放在单独的脚本中,如下所示。

      "scripts": {
         "parallelexample2": "npm-run-all echo*",
          "echo1": "echo 1",
          "echo2": "echo 2",
          "echo3": "echo 3"
      },
      

      以下命令:

      npm run parallelexample2
      

      适用于 windows 和 unix(Linux/MacOS)。

      【讨论】:

      • 感谢 npm-run-all 的提示,我也尝试了 --parallel 选项
      【解决方案6】:

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

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

      $ npm config ls -l | grep shell
      

      默认情况下,输出将是:

      shell = "C:\\WINDOWS\\system32\\cmd.exe"
      

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

      shell = "C:\\Program Files (x86)\\git\\bin\\bash.exe"
      

      您使用的路径可以是 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)
      

      也许有一天,新版本的 npm 会注意覆盖的 shell 参数。

      【讨论】:

      • 希望这能奏效。我更改了 COMSPEC 环境变量,它似乎是从中提取的,但我无法让它正常工作。
      • 这做同样的工作:$ npm config set shell "C:\\msys64\\usr\\bin\\bash"
      • 我按照您的建议解决了我的问题。现在,我可以在 npm 上运行包含更多由 ; 分隔的命令的脚本了
      【解决方案7】:

      这是一种方法:

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

        "scripts": {
          "boogie": "bash bin/my_script.sh"
        }
        

      现在您可以通过以下方式从 npm 运行 bash 脚本:

          npm run-script boogie
      

      不是很优雅,但很有效。

      如果您同时在 Windows 和 Linux/Unix 中进行开发,那么至少这种方法对于这两种环境都相当可移植。

      【讨论】:

      • 另一种选择:"scripts": { "start": "node ./bin/www", "bash": "bash -c", "ls": "npm run bash ls" }
      猜你喜欢
      • 2018-07-01
      • 2017-09-09
      • 2014-04-20
      • 2019-06-07
      • 1970-01-01
      • 2019-11-30
      • 2017-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多