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.
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
- Run the exact production build command CI uses.
- Fix AOT template errors and mark side-effectful files in package.json sideEffects if tree-shaking removed needed code.
- Re-run until the production build is clean.
npx ng build --configuration productionDeclare side effects so the optimizer keeps them
List files with intentional side effects so the optimizer does not tree-shake them away.
"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.