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

如何将已生成的NX库设置为可构建?

修复NX未设置为可构建状态的库

步骤1:检查当前库的配置

进入你的库所在目录,打开project.json文件,确认是否存在targets.build节点。如果没有,说明该库未配置构建能力。

步骤2:添加构建器配置

根据库的类型(TypeScript工具库、React组件库等),在project.json的targets字段中添加对应的构建目标:

TypeScript库(使用@nx/js:tsc构建器)

"build": {
  "executor": "@nx/js:tsc",
  "outputs": ["{options.outputPath}"],
  "options": {
    "outputPath": "dist/libs/[你的库名称]",
    "main": "libs/[你的库名称]/src/index.ts",
    "tsConfig": "libs/[你的库名称]/tsconfig.lib.json",
    "assets": ["libs/[你的库名称]/*.md"]
  }
}

React组件库(使用@nx/react:swc构建器)

"build": {
  "executor": "@nx/react:swc",
  "outputs": ["{options.outputPath}"],
  "options": {
    "outputPath": "dist/libs/[你的库名称]",
    "main": "libs/[你的库名称]/src/index.ts",
    "tsConfig": "libs/[你的库名称]/tsconfig.lib.json",
    "assets": ["libs/[你的库名称]/*.md", "libs/[你的库名称]/src/**/*.css"]
  }
}

替换[你的库名称]为实际库名,同时根据项目结构调整main、tsConfig、assets的路径。

步骤3:测试构建

在终端执行以下命令验证配置是否生效:

nx build [你的库名称]

如果构建成功,说明已修复;若报错,根据终端提示调整project.json中的路径或构建参数。

可选:补充lint、test目标

如果库缺少代码检查或测试目标,可在targets中添加:

"lint": {
  "executor": "@nx/linter:eslint",
  "outputs": ["{options.outputFile}"],
  "options": {
    "lintFilePatterns": ["libs/[你的库名称]/**/*.ts", "libs/[你的库名称]/**/*.tsx"]
  }
},
"test": {
  "executor": "@nx/jest:jest",
  "outputs": ["{workspaceRoot}/coverage/libs/[你的库名称]"],
  "options": {
    "jestConfig": "libs/[你的库名称]/jest.config.ts",
    "passWithNoTests": true
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 13:51:15