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

Angular 17添加PWA执行ng build报错:Data path不可含ngswConfigPath属性

Angular 17 添加PWA后构建报错:Schema验证失败(ngswConfigPath)

问题描述

从angular.io下载示例项目后,执行以下命令:

ng add @angular/pwa   
ng build

运行ng build时触发Schema验证错误,提示:

Data path "" must NOT have additional properties(ngswConfigPath).

完整报错信息:

Error: Schema validation failed with the following errors:
  Data path "" must NOT have additional properties(ngswConfigPath).
E:\Personal Project\service-worker-example\node_modules\rxjs\dist\cjs\internal\util\reportUnhandledError.js:13
            throw err;
            ^

SchemaValidationException [Error]: Schema validation failed with the following errors:
  Data path "" must NOT have additional properties(ngswConfigPath).
    at E:\Personal Project\service-worker-example\node_modules\@angular-devkit\architect\src\architect.js:40:27 {
  errors: [
    {
      instancePath: '',
      schemaPath: '#/additionalProperties',
      keyword: 'additionalProperties',
      params: { additionalProperty: 'ngswConfigPath' },
      message: 'must NOT have additional properties'
    }
  ]
}

当前使用Angular 17版本,已尝试删除node_modules并重新安装,问题仍未解决。

当前angular.json配置

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "angular.io-example": {
      "projectType": "application",
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:application",
          "options": {
            "outputPath": "dist",
            "index": "src/index.html",
            "browser": "src/main.ts",
            "polyfills": ["zone.js"],
            "tsConfig": "tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets",
              "src/manifest.webmanifest"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": [],
            "serviceWorker": true,
            "ngswConfigPath": "ngsw-config.json"
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "outputHashing": "all",
              "serviceWorker": "ngsw-config.json"
            },
            "development": {
              "optimization": false,
              "extractLicenses": false,
              "sourceMap": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "buildTarget": "angular.io-example:build:production"
            },
            "development": {
              "buildTarget": "angular.io-example:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "buildTarget": "angular.io-example:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "polyfills": ["zone.js", "zone.js/testing"],
            "tsConfig": "tsconfig.spec.json",
            "assets": [
              "src/favicon.ico",
              "src/assets",
              "src/manifest.webmanifest"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "angular.io-example:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "angular.io-example:serve:production"
            }
          }
        }
      }
    }
  },
  "cli": {
    "analytics": "7cf21382-de23-496e-9cb0-b31c10d2ff0d"
  }
}

解决方案

问题源于Angular 17使用的**新application构建器(@angular-devkit/build-angular:application)**重构了配置结构,旧版本的根级ngswConfigPath和字符串格式serviceWorker配置不再被支持,需按以下步骤修改:

步骤1:调整build.options中的浏览器配置

将原根级的serviceWorker: true和ngswConfigPath: "ngsw-config.json",以及字符串形式的browser: "src/main.ts",替换为对象形式的浏览器配置:

"browser": {
  "input": "src/main.ts",
  "serviceWorker": {
    "enabled": true,
    "config": "ngsw-config.json"
  }
},

步骤2:修正production环境的serviceWorker配置

将production配置中的serviceWorker: "ngsw-config.json"改为对象格式:

"serviceWorker": {
  "enabled": true,
  "config": "ngsw-config.json"
}

修改后的完整build配置片段

"build": {
  "builder": "@angular-devkit/build-angular:application",
  "options": {
    "outputPath": "dist",
    "index": "src/index.html",
    "browser": {
      "input": "src/main.ts",
      "serviceWorker": {
        "enabled": true,
        "config": "ngsw-config.json"
      }
    },
    "polyfills": ["zone.js"],
    "tsConfig": "tsconfig.app.json",
    "assets": [
      "src/favicon.ico",
      "src/assets",
      "src/manifest.webmanifest"
    ],
    "styles": [
      "src/styles.css"
    ],
    "scripts": []
  },
  "configurations": {
    "production": {
      "budgets": [
        {
          "type": "initial",
          "maximumWarning": "500kb",
          "maximumError": "1mb"
        },
        {
          "type": "anyComponentStyle",
          "maximumWarning": "2kb",
          "maximumError": "4kb"
        }
      ],
      "outputHashing": "all",
      "serviceWorker": {
        "enabled": true,
        "config": "ngsw-config.json"
      }
    },
    "development": {
      "optimization": false,
      "extractLicenses": false,
      "sourceMap": true
    }
  },
  "defaultConfiguration": "production"
}

步骤3:重新执行构建

保存修改后的angular.json,再次运行:

ng build

原因说明

Angular 17的新应用构建器将浏览器相关配置(包括Service Worker)统一归集到browser对象下,废弃了旧构建器中根级的ngswConfigPath参数,同时要求serviceWorker配置必须为对象格式而非字符串或布尔值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 13:45:31