Angular 5执行ng build --prod时出现编译内部错误求助
unknown identifier undefined Angular Production Build Error Hey there, let's work through this frustrating build error together. That unknown identifier undefined message from the Angular compiler almost always ties back to misconfigurations in your dependency injection setup or module declarations. Here are targeted steps to track down and fix the issue:
Audit your NgModule providers
Dig into every@NgModulein your project, especially theprovidersarray. Look for:- Entries referencing a service or token that wasn’t imported (e.g.,
providers: [MyService]but noimport { MyService } from './my.service'). - Providers using
useValueoruseFactorythat point to an undefined value (like{ provide: APP_CONFIG, useValue: undefined }). - Typos in service or token names (a small typo can make the compiler look for an undefined identifier).
- Entries referencing a service or token that wasn’t imported (e.g.,
Check lazy-loaded module dependencies
If you’re using lazy loading, ensure all tokens and services referenced in lazy modules are properly imported or provided. It’s easy to accidentally reference a root-only token in a lazy module without importing it first.Hunt for circular dependencies
Circular loops between services can trigger this obscure compiler error. Runng build --prod --verboseto get more detailed logs—this often points to the specific service pair causing the loop. You can refactor the services to break the cycle (e.g., usingInjectorto lazy-load one of the dependencies).Update Angular core packages
Outdated versions of@angular/compileror core Angular libraries sometimes have bugs that cause this issue. Update to the latest stable version with these commands:npm install @angular/cli@latest @angular/core@latest @angular/compiler@latest --save npm installClean build artifacts and cache
Stale cache or corrupted node modules can lead to weird compiler glitches. Wipe them clean and rebuild:ng clean rm -rf node_modules package-lock.json npm install ng build --prodTest without AOT (for debugging)
Temporarily runng build --prod --aot=falseto see if the error disappears. If it does, the problem is definitely in code that AOT compilation is stricter about—circle back to your provider and module configurations to find the issue.
内容的提问来源于stack exchange,提问作者Alexandr

