You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular 6中angular.json添加环境配置后运行报错的解决咨询

Angular 6 CLI: Fixing "Either route or analyzedModules has to be specified!" Error with Local Environment Configuration

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:

  1. Update the local build configuration in angular.json:
    Replace your current local config under build.configurations with 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
    }
    
  2. Confirm the serve configuration (yours is already correct):
    The serve.configurations.local entry correctly points to bb-admin:build:local, so no changes are needed here.

  3. Test the command:
    Run ng 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) for ng serve

内容的提问来源于stack exchange,提问作者slipperypete

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:56:08