如何並行運行多個 npm 腳本?
P粉807239416
P粉807239416 2023-08-23 17:10:35
0
2
577
<p>在我的 <code>package.json</code> 中,我有這兩個腳本:</p> <pre class="brush:php;toolbar:false;">"scripts": { "start-watch": "nodemon run-babel index.js", "wp-server": "webpack-dev-server", }</pre> <p>每次開始在 Node.js 中開發時,我都必須<strong>並行</strong>運行這兩個腳本。我首先想到的是添加第三個腳本,如下所示:</p> <pre class="brush:php;toolbar:false;">"dev": "npm run start-watch && npm run wp-server"</pre> <p>...但這將等待 <code>start-watch</code> 完成後再執行 <code>wp-server</code>。 </p> <p><strong>如何並行執行這些指令? </strong>請記住,我需要查看這些命令的<code>輸出</code>。另外,如果您的解決方案涉及建置工具,我寧願使用 <code>gulp</code> 而不是 <code>grunt</code>,因為我已經在另一個專案中使用它。 </p>
P粉807239416
P粉807239416

全部回覆(2)
P粉026665919

如果您使用的是類別 UNIX 環境,只需使用 & 作為分隔符號:

"dev": "npm run start-watch & npm run wp-server"

否則,如果您對跨平台解決方案感興趣,可以使用 npm-run-all模組:

"dev": "npm-run-all --parallel start-watch wp-server"
P粉143640496

使用名為並發的套件。

npm i concurrently --save-dev

然後如下設定您的 npm run dev 任務:

"dev": "concurrently --kill-others \"npm run start-watch\" \"npm run wp-server\""
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板