Angular 7 App ng build Works But ng build --prod Gives Error
A frustrating Angular development experience is having ng build succeed while ng build --prod fails with cryptic errors. This happens because the production build enables additional optimizations and stricter checks that the development build skips.
Here are the most common causes and their fixes.
Problem 1: Strict Parameter Type Checking
Production builds enforce stricter type checking on template event bindings. If a template passes an event argument that the handler does not accept, the production compiler rejects it.
Template:
<app-component (someEvent)="handleEvent($event)"></app-component>
Component method missing the parameter:
// Incorrect — missing $event parameter
handleEvent() {
// ...
}
Fix: Add the matching parameter to the method signature:
handleEvent(event: any) {
// ...
}
Problem 2: Duplicate Component Declarations
If a component is declared in more than one NgModule, the development build may tolerate it but the production build will throw:
Type SomeComponent is part of the declarations of 2 modules
Fix: Each component should be declared in exactly one module. Move shared components into a shared module and import that module wherever needed — do not redeclare the component.
Problem 3: Bootstrap Module Defined Twice
The error Cannot read property 'moduleType' of undefined usually means bootstrapModule is called more than once in your entry point:
// main.ts — incorrect: called twice
platformBrowserDynamic().bootstrapModule(AppModule);
platformBrowserDynamic().bootstrapModule(AppModule);
Fix: Ensure bootstrapModule is called exactly once in main.ts:
platformBrowserDynamic()
.bootstrapModule(AppModule)
.catch(err => console.error(err));
Problem 4: Enable Ivy Compilation
Angular 7 ships with Ivy disabled by default. Enabling it can resolve certain production build failures and significantly reduces bundle size. Add the following to tsconfig.json:
{
"angularCompilerOptions": {
"enableIvy": true
}
}
Ivy's stricter compilation surface area sometimes exposes issues that the older View Engine compiler missed. If enabling Ivy surfaces new errors, they indicate real problems that were previously hidden and should be fixed rather than avoided.
Debugging Tips
- Run
ng build --prod --source-mapto get readable stack traces. - The AOT compiler (
--aot, which is the default in prod) is stricter than the JIT compiler used in development — most prod-only failures are AOT compilation errors. - Run
ng build --prod --verbosefor detailed output on which file is causing the failure.
Conclusion
Production build failures in Angular 7 almost always trace back to stricter AOT compilation rules catching issues that JIT compilation lets slide. Work through each error systematically: fix parameter mismatches, eliminate duplicate declarations, ensure a single bootstrap call, and consider enabling Ivy for both performance gains and better compile-time feedback.