Skip to content
Latchkey

Angular optimization / production build failures in CI

The production configuration turns on AOT, optimization, and stricter output than a dev build. Code that runs in ng serve can fail ng build --configuration production in CI because the optimizer and AOT enforce contracts the dev build does not.

What this error means

ng build --configuration production fails (AOT template errors, minifier errors, or unexpected empty output) while ng serve and dev builds work locally.

ng build
Error: src/app/app.component.html:2:5 - error NG5002:
Parser Error: ... (only surfaced under AOT production build)

Common causes

Dev build uses JIT; production uses AOT

Template and metadata problems that JIT tolerates at runtime become hard compile errors under the AOT production build.

Optimizer assumptions broken by side-effectful code

Aggressive optimization and tree-shaking can drop or mangle code that relies on side effects the optimizer assumes are absent.

How to fix it

Build production locally to reproduce

  1. Run the exact production build command CI uses.
  2. Fix AOT template errors and mark side-effectful files in package.json sideEffects if tree-shaking removed needed code.
  3. Re-run until the production build is clean.
Terminal
npx ng build --configuration production

Declare side effects so the optimizer keeps them

List files with intentional side effects so the optimizer does not tree-shake them away.

package.json
"sideEffects": ["*.css", "src/polyfills.ts"]

How to prevent it

  • Build the production configuration locally before pushing.
  • Keep AOT on in development to catch template errors early.
  • Declare sideEffects accurately so optimization is safe.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →