搜尋

首頁  >  問答  >  主體

如何並行運行多個 npm 腳本?

<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粉807239416P粉807239416582 天前610

全部回覆(2)我來回復

  • P粉026665919

    P粉0266659192023-08-24 10:31:34

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

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

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

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

    回覆
    0
  • P粉143640496

    P粉1436404962023-08-24 00:58:21

    使用名為並發的套件。

    npm i concurrently --save-dev

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

    "dev": "concurrently --kill-others \"npm run start-watch\" \"npm run wp-server\""

    回覆
    0
  • 取消回覆