Angular 10执行ng build --prod时出现Call retries were exceeded报错求助
Angular 10生产构建在差异加载阶段报错:Call retries were exceeded
我的Angular 10项目规模逐渐增大,执行ng build --prod构建时开始报错。构建到Generating ES5 bundles for differential loading...阶段失败,报错信息如下:
An unhandled exception occurred: Call retries were exceeded See "...\angular-errors.log" for further details.
错误日志:
[error] Error: Call retries were exceeded at ChildProcessWorker.initialize (...\node_modules\jest-worker\build\workers\ChildProcessWorker.js:193:21) at ChildProcessWorker._onExit (...\node_modules\jest-worker\build\workers\ChildProcessWorker.js:274:12) at ChildProcess.emit (events.js:400:28) at Process.ChildProcess._handle.onexit (internal/child_process.js:285:12)
注释掉任意组件中约1000行代码后可成功构建,但恢复代码后报错重现。必须保留差异加载功能,无法禁用。
环境信息
- Node.js版本:
v14.21.2 - Angular版本:
~10.2.0
tsconfig.json配置
{ "compileOnSave": false, "compilerOptions": { "baseUrl": "./", "outDir": "./dist/out-tsc", "sourceMap": true, "declaration": false, "downlevelIteration": true, "experimentalDecorators": true, "moduleResolution": "node", "importHelpers": true, "target": "es2015", "module": "es2020", "lib": [ "es2019", "es2018", "dom" ] } }
已尝试的方案
- 无效方案:
- 将构建进程内存提升至12GB
- 在package.json中添加
"sideEffects": false - 删除并重新安装node_modules
- 临时有效但不符合需求:
- 在tsconfig.json中设置
"target": "es5"(会禁用差异加载)
- 在tsconfig.json中设置
可行解决方案
1. 限制构建并行worker数量
在angular.json的项目构建配置中添加maxWorkers参数,减少并行处理的压力,避免worker进程重试超限:
"projects": { "your-project-name": { "architect": { "build": { "options": { // 原有配置 "maxWorkers": 2 } } } } }
可根据CPU核心数调整数值,比如双核机器设为1或2。
2. 拆分大型模块为懒加载
对体积较大的组件、模块实施路由懒加载,降低单次构建需要处理的代码量:
// app-routing.module.ts const routes: Routes = [ // 替换原有模块引入方式为懒加载 { path: 'heavy-feature', loadChildren: () => import('./heavy-feature/heavy-feature.module').then(m => m.HeavyFeatureModule) } ];
3. 升级jest-worker依赖
报错源于jest-worker的重试机制问题,手动升级到兼容Angular 10的稳定版本:
npm install jest-worker@^27.5.1 --save-dev
4. 启用构建缓存
在angular.json的构建配置中开启缓存,复用之前的编译结果:
"projects": { "your-project-name": { "architect": { "build": { "options": { // 原有配置 "cache": true } } } } }
5. 优化TS编译配置
保留target: es2015的前提下,在angular.json中声明允许的CommonJS依赖,减少编译转换的复杂度:
"projects": { "your-project-name": { "architect": { "build": { "options": { // 原有配置 "allowedCommonJsDependencies": ["your-commonjs-dep"] } } } } }
内容的提问来源于stack exchange,提问作者YSFKBDY
相关产品推荐
相关产品推荐

