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

如何为NX项目同时运行不同的目标脚本?

解决方案

当需要为不同项目指定不同运行目标时,nx run-many无法直接实现(它要求所有项目使用同一个--target),可以用以下几种方式解决:

  • 直接终端并行执行
    无需额外配置,直接在终端同时启动两个独立的NX命令,不同系统语法略有区别:
    Unix/Linux/macOS 终端:

    nx run server:serve & nx run client:serve-ssr
    

    Windows CMD:

    start /B nx run server:serve && nx run client:serve-ssr
    

    Windows PowerShell:

    Start-Process nx -ArgumentList "run server:serve" -NoNewWindow; nx run client:serve-ssr
    
  • 自定义package.json脚本(跨平台友好)
    先安装跨平台并行工具concurrently:

    npm install -D concurrently
    

    然后在根目录package.json的scripts字段中添加自定义命令:

    "scripts": {
      // ...其他脚本
      "serve:combined": "concurrently \"nx run server:serve\" \"nx run client:serve-ssr\""
    }
    

    之后只需执行一次命令即可:

    npm run serve:combined
    
  • NX复合任务配置(推荐)
    在根目录的nx.json中定义一个复合目标,让它并行依赖两个项目的对应任务:

    "targetDefaults": {
      // ...其他配置
      "serve:combined": {
        "dependsOn": [
          "server:serve",
          "client:serve-ssr"
        ],
        "parallel": true
      }
    }
    

    执行时直接调用这个复合目标:

    nx run-many --target=serve:combined --projects=server,client
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 09:55:19