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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23243353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:32:13  来源:igfitidea点击:

How to set shell for npm run-scripts in Windows

windowsshellnpm

提问by user2129444

I'm running npm on Windows and would like to use & style parallel operations in run-scripts but running in parallel in cmd is kind of messy in my package.json file I'd like to write-

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

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

but npm executes the script under cmd.exe which does not know about ;I could change this to scripts: { "go": "bats/bat1.bat")where bat1.bat is a cmd bat file that uses the windows style call or start commands to run commands in parallel. which works but gives me a script that only works on Windows.

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

It would be a lot simpler if I could get npm to run the script under a bash clone or cygwin.

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

I tried config: { "shell": "bash"}but that still ran cmd.exe

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

Is there any way to tell npm to run-scripts using a specific shell (not cmd.exe)?

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

回答by DuncanSungWKim

Since npm 5.1

从 npm 5.1 开始

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

or (64bit installation)

(64位安装)

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

Note that you need to have git for windows installed.

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

You can revert it by running:

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

npm config delete script-shell

回答by Craig Johannsen

Here's one way to do it:

这是一种方法:

  1. Create a script, such as my_script.sh, in your project bin directory.
  2. In your package.json file, add a line to run the script using bash. For example:

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

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

Now you can run your bash script from npm by:

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

    npm run-script boogie

Not very elegant, but it works.

不是很优雅,但它有效。

If you are developing in both Windows and Linux/Unix, then at least this approach is fairly portable to both environments.

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

回答by Craig Johannsen

Ideally, overriding the npm shell config parameter should work, but npm (at least version 1.4.14) seems in Windows to ignore the setting and use cmd.exe instead.

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

Use the following command in your bash or Git Bash shell to find out the shell setting:

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

$ npm config ls -l | grep shell

By default, the output will be:

默认情况下,输出将是:

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

However, to override the default shell parameter, you can add (or edit) an npmrc file to the \Users\yourusername\AppData\Roaming\npm\etc directory. Just add the following line:

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

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

The path you use can be any valid path to bash.exe. Now, if you run the above "npm config ls -l | grep shell" command, you will see the following output, indicating that the shell parameter has been overriden:

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

One day, perhaps, a new version of npm will pay attention to the overridden shell parameter.

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

回答by Atilla Ozgur

Use a specifically created node_module for this purpose. I suggest using npm-run-all, but othersexists, such as parallelshell.

为此使用专门创建的 node_module。我建议使用npm-run-all,但也有其他的,比如parallelshell

Parallelshell example is below for drop-in-replacement for your question.

下面的 Parallelshell 示例用于直接替换您的问题。

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

following command:

以下命令:

npm run parallelexample1

works both on windows and unix(Linux/MacOS).

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

Interestingly npm-run-all does not support shell commands; therefore we need to put all shell commands to separate scripts like below.

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

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

Following command:

以下命令:

npm run parallelexample2

works both on windows and unix(Linux/MacOS).

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

回答by Atilla Ozgur

just using CMD's way to run .bat!

只需使用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/

回答by Mike Keskinov

In my case I just needed to run npm startfrom inside Bash. I run cmdthen I open bash by running "c:\Program Files\Git\bin\bash.exe". Under bash shell I then was able to call npm buildand npm startsuccesfully.

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

You may already have bash if you are using Git. If not, you can installit.

如果您使用 Git,您可能已经拥有 bash。如果没有,您可以安装它。

Hope this may save someone's time.

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