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

本地Docker构建正常,AWS CodeBuild中Angular编译报Optimization错误

问题:AWS CodeBuild中Angular构建报Optimization error [main.js]: Error: The service was stopped

本地执行docker build -t my-app .构建Node.js嵌入Angular的应用镜像无异常,但在AWS CodeBuild流水线中执行相同命令时,/app/ng-app目录下的npm run build抛出优化错误,已确认本地与CodeBuild环境Node版本一致。


相关文件信息

Dockerfile

FROM node:12.14.1-alpine

WORKDIR /app

COPY . ./

WORKDIR /app/ng-app

RUN npm install && npm run build


WORKDIR /app

# npm install and delete everything from the ng-app folder except the out directory.
RUN npm install 
RUN find ./ng-app -depth -mindepth 1 -maxdepth 1  -not -name out -exec rm -rf '{}' \;

EXPOSE 3000

CMD [ "npm", "start" ]

报错日志

Compiling @angular/flex-layout/extended : es2015 as esm2015
Compiling @angular/flex-layout/flex : es2015 as esm2015
Compiling @angular/flex-layout/grid : es2015 as esm2015
Compiling @angular/flex-layout : es2015 as esm2015
Compiling ngx-scrollbar/smooth-scroll : es2015 as esm2015
Compiling ngx-scrollbar : es2015 as esm2015

✔ Browser application bundle generation complete.
✔ Browser application bundle generation complete.


Warning: /app/ng-app/node_modules/canvg/lib/index.es.js depends on 'core-js/modules/es.array.index-of.js'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies

Warning: /app/ng-app/node_modules/canvg/lib/index.es.js depends on 'core-js/modules/es.array.iterator.js'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies

Warning: /app/ng-app/src/app/board/service/paper.service.ts depends on 'tree-util'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies


Error: Optimization error [main.js]: Error: The service was stopped
    at /app/ng-app/node_modules/@angular-devkit/build-angular/node_modules/esbuild/lib/main.js:1276:29
    at /app/ng-app/node_modules/@angular-devkit/build-angular/node_modules/esbuild/lib/main.js:629:9
    at Socket.afterClose (/app/ng-app/node_modules/@angular-devkit/build-angular/node_modules/esbuild/lib/main.js:607:7)
    at Socket.emit (events.js:228:7)
    at endReadableNT (_stream_readable.js:1185:12)
    at processTicksAndRejections (internal/process/task_queues.js:81:21)


npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! my-app@0.0.0 build: `ng build --output-hashing none`
npm ERR! Exit status 1

本地Angular CLI版本

Angular CLI: 12.2.17
Node: 12.14.1
Package Manager: npm 6.13.4
OS: darwin x64

Angular: 12.2.16
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1202.17
@angular-devkit/build-angular   12.2.17
@angular-devkit/core            12.2.17
@angular-devkit/schematics      12.2.17
@angular/cdk                    12.2.13
@angular/cli                    12.2.17
@angular/flex-layout            12.0.0-beta.35
@angular/material               12.2.13
@schematics/angular             12.2.17
rxjs                            6.6.7
typescript                      4.3.5

解决方案

1. 提升CodeBuild资源规格

错误核心原因是CodeBuild默认资源的内存不足,导致esbuild优化进程被系统终止:

  • 将CodeBuild项目的构建环境从BUILD_GENERAL1_SMALL切换为BUILD_GENERAL1_MEDIUM或更高规格
  • 若使用自定义构建镜像,确保容器分配足够内存

2. 临时关闭构建优化(应急方案)

修改Angular项目package.json的build命令,跳过优化阶段:

"scripts": {
  "build": "ng build --output-hashing none --optimization=false"
}

注意:该方案会增大构建产物体积,仅作临时验证或应急使用,不建议生产环境长期采用。

3. 增加Node进程内存分配

在Dockerfile中添加环境变量,给Node进程分配更多内存:

WORKDIR /app/ng-app
ENV NODE_OPTIONS="--max-old-space-size=4096"
RUN npm install && npm run build

该配置给Node分配4GB内存,适配Angular构建优化阶段的内存需求。

4. 处理CommonJS依赖警告

CommonJS依赖会导致构建优化阶段内存消耗激增,通过以下方式处理:

  • 替换tree-util为ES模块版本的替代库
  • 在angular.json的build配置中添加允许的CommonJS依赖,避免优化 bailout:
"build": {
  "builder": "@angular-devkit/build-angular:browser",
  "options": {
    // 其他原有配置
    "allowedCommonJsDependencies": [
      "core-js/modules/es.array.index-of.js",
      "core-js/modules/es.array.iterator.js",
      "tree-util"
    ]
  }
}

内容的提问来源于stack exchange,提问作者A J Qarshi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 16:25:35