You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Nx在Monorepo中并行运行Build Watch与Serve命令

解决方案

方法1:根package.json自定义脚本(跨平台推荐)

  1. 安装concurrently(未安装的话执行):
    npm install concurrently --save-dev
    # 或 yarn add concurrently -D
    # 或 pnpm add concurrently -D
    
  2. 在根目录package.json中添加脚本:
    {
      "scripts": {
        "dev": "concurrently \"nx run elements:build --watch\" \"nx run website:serve\""
      }
    }
    
  3. 执行命令启动:
    npm run dev
    

方法2:直接执行并行命令(适合类Unix环境)

在终端直接运行:

nx run elements:build --watch & nx run website:serve

注意:Windows CMD不支持&并行,PowerShell可改用方法1或使用Start-Process单独启动每个命令。

方法3:Nx复合任务配置(贴合Nx生态)

在nx.json中定义复合目标,绑定两个任务:

{
  "targetDefaults": {
    "dev": {
      "dependsOn": ["elements:build --watch", "website:serve"],
      "parallel": true
    }
  }
}

然后执行:

nx run-many --target=dev --projects=elements,website

前提检查

  • 确认elements包的project.json中已配置build目标,且支持--watch参数(多数Nx执行器默认支持)。
  • 确认website包的project.json中已配置serve目标,可正常启动开发服务。

内容的提问来源于stack exchange,提问作者captDaylight

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 01:35:45