Angular 5生产构建报错:Either route or analyzedModules has to be specified!
Hey there, let's break down how to fix that frustrating AOT compilation error you're seeing when running ng build --prod with Angular 5.2. Local dev builds work fine, so this is definitely an AOT-specific issue—here are the most common places to look:
1. Audit Your Route Configurations
AOT is way stricter about route validity than the JIT compiler. Start here because this error often ties back to malformed routes:
- Check for empty or incomplete route objects in your
app-routing.module.ts(or any feature route files). Every route should have either acomponentproperty or aloadChildren(for lazy loading) defined—no blank entries allowed. - Look for typos in component paths or lazy load module strings. For example, if you have
loadChildren: './lazy/lazy.module#LazyModule', make sure the path and module name are 100% correct. A tiny typo here can break AOT. - Try commenting out sections of your routes one by one and re-running
ng build --prodto isolate which route is causing the failure.
2. Verify Module Declarations & Exports
AOT needs clear metadata about every component used in your app. If you're referencing a component in a route that isn't properly declared or exported, it can trigger this error:
- For any component used in routes, confirm it's listed in the
declarationsarray of its parent NgModule. If it's shared across modules, make sure it's exported from its declaring module and imported where needed. - Double-check lazy-loaded modules: Ensure their own routing configurations are valid, and that the module itself is properly decorated with
@NgModule(no missing imports/declarations). - If you're using third-party libraries, make sure they're compatible with AOT. Some older libraries don't ship with the necessary metadata—try updating the library to its latest Angular 5-compatible version, or temporarily add
"skipMetadataCheck": trueto yourtsconfig.json'sangularCompilerOptions(note: this is a band-aid, not a long-term fix).
3. Align Angular CLI & Core Dependency Versions
Mismatched versions between @angular/cli and your core Angular packages (like @angular/core, @angular/compiler) can cause AOT compilation bugs:
- Run
npm list @angular/core @angular/cliin your terminal to check versions. Both should be in the 5.2.x range. - If they're out of sync, delete your
node_modulesfolder andpackage-lock.json/yarn.lock, then reinstall dependencies with exact versions (e.g.,npm install @angular/core@5.2.11 @angular/cli@1.7.4—adjust to the latest 5.2.x releases).
4. Clean Caches & Rebuild from Scratch
Sometimes stale cache files cause weird AOT glitches. Let's wipe everything and start fresh:
- Delete the
distfolder,node_modules, andpackage-lock.json. - Run
npm installto reinstall dependencies. - Try building with the
--delete-output-pathflag to ensure a clean build:ng build --prod --delete-output-path.
5. Enable Verbose Logging for Clues
The default error message is vague—get more details with verbose logging:
- Run
ng build --prod --verboseto see a full breakdown of the compilation process. Look for any warnings or errors that appear right before the "Either route or analyzedModules..." message—this often points to the problematic module or route.
6. Check Your tsconfig.json Compiler Options
Incorrect Angular compiler settings can throw off AOT:
- Open your
tsconfig.jsonand verify theangularCompilerOptionssection. For Angular 5, the default settings should include"enableIvy": false(Ivy wasn't available yet) and"strictMetadataEmit": true. - If you've modified these options, try resetting them to the default Angular 5 configuration to rule out misconfigurations.
内容的提问来源于stack exchange,提问作者P Toyn

