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

Angular 15多次级入口构建报错:无法从内存加载依赖文件

解决方案:Angular 15库ES模块构建失败问题

针对你遇到的Angular 15下ES2020模块构建失败、源码残留临时文件的问题,可尝试以下几个方向排查修复:

1. 统一Devkit依赖版本

当前环境中@angular-devkit/schematics版本为13.3.10,与其他Angular Devkit包(15.2.2)版本不一致,这可能导致构建流程中的模块解析异常。执行以下命令升级该包:

npm install @angular-devkit/schematics@15.2.2 --save-dev

2. 清理构建缓存与临时文件

源码目录残留的编译产物会干扰构建流程,按以下步骤清理:

  • 手动删除dist目录、源码中残留的.mjs/.d.ts/.d.ts.map等临时文件
  • 执行Angular缓存清理命令:
ng clean
  • 清理npm缓存(可选):
npm cache clean --force

完成后重新执行构建。

3. 检查次级入口配置

每个次级入口的package.json需确保模块路径配置正确,以controls入口为例:

{
  "main": "../dist/components/controls/bundles/controls.umd.js",
  "module": "../dist/components/esm2020/controls/controls.mjs",
  "typings": "../dist/components/controls/controls.d.ts"
}
  • 确认module字段指向ES2020格式的输出文件,路径与ng-packagr的输出结构一致
  • 次级入口间的依赖导入需使用包内路径(如import { ControlsService } from '@your-lib/controls'),避免使用相对路径导致解析混乱

4. 调整TypeScript配置

修改库的tsconfig.lib.json:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ES2020",
    "moduleResolution": "node16",
    "outDir": "../../out-tsc/lib",
    // 其他配置...
  }
}
  • 设置moduleResolution为node16(适配Node 18的ES模块解析规则)
  • 确保outDir指向临时编译目录,而非源码目录,避免产物混入源码

5. 优化ng-packagr配置

在根目录的ng-package.json中,确保次级入口配置正确,同时指定输出格式:

{
  "dest": "./dist/components",
  "lib": {
    "entryFile": "src/public-api.ts"
  },
  "secondaryEntryPoints": [
    "./src/components/controls",
    "./src/components/form"
  ],
  "formats": ["esm2020", "umd"]
}
  • 确认secondaryEntryPoints的路径指向正确的源码目录
  • 显式指定formats字段,确保ng-packagr只生成所需的模块格式

如果以上方法仍未解决,可尝试临时禁用Ivy的全编译模式,在tsconfig.lib.json中添加:

{
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": false,
    "skipTemplateCodegen": true
  }
}

但这只是临时方案,建议后续在修复其他问题后恢复Ivy全编译。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 16:10:33