Angular 6中angular.json添加环境配置后运行报错的解决咨询
I recently upgraded to Angular CLI 6 and tried setting up a local environment configuration in angular.json to replicate the old ng serve --env=local behavior. I added a local configuration under both build and serve sections, but when running ng serve --configuration=local, I hit this error:
ERROR in Either route or analyzedModules has to be specified!
Here's my current angular.json configuration (relevant sections):
{ "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "version": 1, "newProjectRoot": "projects", "projects": { "bb-admin": { "root": "", "sourceRoot": "src", "projectType": "application", "prefix": "app", "schematics": { "@schematics/angular:component": { "styleext": "sass" } }, "architect": { "build": { "builder": "@angular-devkit/build-angular:browser", "options": { "outputPath": "dist/bb-admin", "index": "src/index.html", "main": "src/main.ts", "polyfills": "src/polyfills.ts", "tsConfig": "src/tsconfig.app.json", "assets": [ "src/favicon.ico", "src/assets" ], "styles": [ "src/styles.sass" ], "scripts": [] }, "configurations": { "production": { "fileReplacements": [ { "replace": "src/environments/environment.ts", "with": "src/environments/environment.prod.ts" } ], "optimization": true, "outputHashing": "all", "sourceMap": false, "extractCss": true, "namedChunks": false, "aot": true, "extractLicenses": true, "vendorChunk": false, "buildOptimizer": true }, "local": { "fileReplacements": [ { "replace": "src/environments/environment.ts", "with": "src/environments/environment.local.ts" } ], "optimization": true, "outputHashing": "all", "sourceMap": false, "extractCss": true, "namedChunks": false, "aot": true, "extractLicenses": true, "vendorChunk": false, "buildOptimizer": true } } }, "serve": { "builder": "@angular-devkit/build-angular:dev-server", "options": { "browserTarget": "bb-admin:build" }, "configurations": { "production": { "browserTarget": "bb-admin:build:production" }, "local": { "browserTarget": "bb-admin:build:local" } } } } } }, "defaultProject": "bb-admin" }
What's Causing the Error?
The issue is that your local build configuration is a direct copy of the production setup—you’ve enabled production-only features like aot: true, buildOptimizer: true, and optimization: true that clash with the Angular Dev Server (ng serve).
The Dev Server is built for fast, iterative development workflows. Forcing production-level optimizations (especially AOT combined with the build optimizer) creates a mismatch in how the server processes the build output, triggering the confusing "Either route or analyzedModules has to be specified!" error.
How to Fix It
You need to adjust the local build configuration to use development-friendly settings while keeping the environment file replacement. Here's the step-by-step fix:
Update the
localbuild configuration inangular.json:
Replace your currentlocalconfig underbuild.configurationswith this development-focused setup:"local": { "fileReplacements": [ { "replace": "src/environments/environment.ts", "with": "src/environments/environment.local.ts" } ], "optimization": false, "outputHashing": "none", "sourceMap": true, "extractCss": false, "namedChunks": true, "aot": false, "extractLicenses": false, "vendorChunk": true, "buildOptimizer": false }Confirm the serve configuration (yours is already correct):
Theserve.configurations.localentry correctly points tobb-admin:build:local, so no changes are needed here.Test the command:
Runng serve --configuration=local—it should now start the dev server using your local environment file without errors.
Bonus: Add a Convenient Script
To avoid typing the full command every time, add a shortcut to your package.json:
"scripts": { // ... existing scripts "start:local": "ng serve --configuration=local" }
Now you can run npm run start:local to launch the local environment in one command.
Key Takeaway for Angular 6+ Environment Configs
Always separate development and production configurations:
- Reserve production settings (AOT, optimizations, output hashing) for
ng build --configuration=production - Keep development-focused configurations (like
local) with debug-friendly defaults (source maps enabled, no optimizations, AOT disabled) forng serve
内容的提问来源于stack exchange,提问作者slipperypete

