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

dauxcl2d  于 5个月前  发布在  Windows
关注(0)|答案(7)|浏览(61)

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

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

字符串
但是npm在;下执行脚本,它不知道;,我可以将其更改为脚本:{ "go": "bats/bat1.bat")其中bat1.bat是一个cmd bat文件,它使用windows样式调用或start命令来并行运行命令。
如果我能让npm在bash clone或cygwin下运行脚本,那就简单多了。
我尝试了config: { "shell": "bash"},但仍然运行config: { "shell": "bash"}
有没有办法告诉npm使用特定的shell(而不是xml.exe)运行脚本?

jgwigjjp

jgwigjjp1#

自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

h6my8fg2

h6my8fg22#

这里有一种方法:
1.在项目bin目录中创建一个脚本,如my_script.sh。
1.在package.json文件中,添加一行代码来使用bash运行脚本。例如:

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

字符串
现在你可以通过以下方式从npm运行bash脚本:

npm run-script boogie


不是很优雅,但它工作。
如果您同时在Windows和Linux/Unix上进行开发,那么至少这种方法在这两种环境下都是相当可移植的。

66bbxpm5

66bbxpm53#

理想情况下,覆盖npm shell配置参数应该可以工作,但npm(至少版本1.4.14)似乎在Windows中忽略了该设置,而是使用npm. 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"


您使用的路径可以是任何有效的路径到npm. 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参数。

bmvo0sr5

bmvo0sr54#

你也可以使用跨平台的powershell https://github.com/powershell/powershell#get-powershell来编写npm脚本。
要为单个项目设置,请从项目根文件夹运行此命令:

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

字符串
要为所有节点项目全局设置,请执行以下操作:

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

jqjz2hbq

jqjz2hbq5#

只是用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/"
},

.bat

start http://localhost:7777/datas/ && start http://localhost:7777/Info/
k0pti3hp

k0pti3hp6#

为此,请使用专门创建的node_module。我建议使用npm-run-all,但others确实存在,例如parallelshell
以下是针对您的问题的drop-in-replacement示例。

"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)。

dl5txlt9

dl5txlt97#

在我的例子中,我只需要在Bash内部运行npm start。我运行cmd,然后通过运行"c:\Program Files\Git\bin\bash.exe"打开bash。在bash shell下,我可以成功地调用npm buildnpm start
如果你使用Git,你可能已经有bash了。如果没有,你可以install它。
希望这可以保存某人的时间。

相关问题