ng build :无法加载工作区配置文件

Posted

技术标签:

【中文标题】ng build :无法加载工作区配置文件【英文标题】:ng build : Workspace config file cannot be loaded 【发布时间】:2020-02-12 23:18:22 【问题描述】:

当我运行 ng build 命令时,我收到以下错误,

发生未处理的异常:无法加载工作区配置文件:C:...\angular\angular.json 无效的 JSON 字符:“”在 0:0。 有关详细信息,请参阅“C:\Users...\AppData\Local\Temp\ng-I8pYWt\angular-errors.log”。

我试过 npm cache clean --force 然后 npm install 或 yarn

angular-errors.log

[error] Error: Workspace config file cannot be loaded: C:\...\angular\angular.json
Invalid JSON character: "" at 0:0.
at Object.getWorkspace (C:\...\angular\node_modules\@angular\cli\utilities\config.js:59:15)
at BuildCommand.validateScope (C:\...\angular\node_modules\@angular\cli\models\command.js:97:60)
at BuildCommand.validateAndRun (C:\...\angular\node_modules\@angular\cli\models\command.js:122:24)
at Object.runCommand (C:\...\angular\node_modules\@angular\cli\models\command-runner.js:186:38)
at process.runNextTicks [as _tickCallback] (internal/process/task_queues.js:52:5)
at Function.Module.runMain (internal/modules/cjs/loader.js:880:11)
at internal/main/run_main_module.js:21:11

版本

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.800.6
@angular-devkit/build-angular     0.800.6
@angular-devkit/build-optimizer   0.800.6
@angular-devkit/build-webpack     0.800.6
@angular-devkit/core              8.3.9
@angular-devkit/schematics        8.3.9
@angular/cdk                      8.2.3
@angular/cli                      8.3.9
@ngtools/webpack                  8.0.6
@schematics/angular               8.3.9
@schematics/update                0.803.9
rxjs                              6.4.0 (cli-only)
typescript                        3.4.5
webpack                           4.30.0

angular.json


  "$schema": "../node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": 
    "abp-zero-template": 
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "architect": 
        "build": 
          "builder": "@angular-devkit/build-angular:browser",
          "options": 
            "outputPath": "dist",
            "index": "src/index.html",
            "main": "src/main.ts",
            "tsConfig": "src/tsconfig.json",
            "polyfills": "src/polyfills.ts",
            "preserveSymlinks": true,
            "assets": [
              "src/assets",
              "src/metronic",
              "src/favicon.ico",
              
                "glob": "abp.signalr-client.js",
                "input": "node_modules/abp-web-resources/Abp/Framework/scripts/libs",
                "output": "/assets/abp"
              
            ],
            "styles": [
              "node_modules/animate.css/animate.min.css",
              "node_modules/quill/dist/quill.core.css",
              "node_modules/quill/dist/quill.snow.css",
              "node_modules/famfamfam-flags/dist/sprite/famfamfam-flags.css",
              "node_modules/primeng/resources/primeng.min.css",
              "node_modules/primeng/resources/themes/nova-light/theme.css",
              "node_modules/primeicons/primeicons.css",
              "node_modules/sweetalert2/dist/sweetalert2.css",
              "node_modules/cookieconsent/build/cookieconsent.min.css",
              "src/assets/FreezeUI/freeze-ui.min.css",

              "src/assets/primeng/file-upload/css/primeng.file-upload.css",
              "src/assets/primeng/autocomplete/css/primeng.autocomplete.css",
              "src/assets/primeng/tree/css/primeng.tree.css",
              "src/assets/primeng/context-menu/css/primeng.context-menu.css",

              "src/assets/fonts/fonts-poppins.css",
              "src/assets/fonts/fonts-roboto.css",

              "src/assets/ngx-bootstrap/bs-datepicker.css",

              "src/assets/metronic/vendors/global/vendors.bundle.css",

              "src/app/shared/core.less",
              "src/app/shared/layout/layout.less",
              "src/styles.css"
            ],
            "scripts": [
              "node_modules/@aspnet/signalr/dist/browser/signalr.min.js",
              "node_modules/localforage/dist/localforage.js",
              "node_modules/moment/min/moment-with-locales.js",
              "node_modules/moment-timezone/builds/moment-timezone-with-data.js",
              "node_modules/push.js/bin/push.js",
              "node_modules/quill/dist/quill.js",
              "node_modules/sweetalert2/dist/sweetalert2.js",
              "node_modules/cookieconsent/build/cookieconsent.min.js",

              "src/assets/FreezeUI/freeze-ui.js",

              "src/assets/abp-web-resources/abp.js",
              "src/assets/abp-web-resources/abp.sweet-alert.js",
              "src/assets/abp-web-resources/abp.notify.js",
              "src/assets/abp-web-resources/abp.freeze-ui.js",
              "node_modules/abp-web-resources/Abp/Framework/scripts/libs/abp.moment.js",

              "src/assets/metronic/vendors/global/vendors.bundle.js",
              "src/assets/metronic/common/js/scripts.bundle.js"
            ]
          ,
          "configurations": 
            "hmr": 
              "fileReplacements": [
                
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.hmr.ts"
                
              ]
            ,
            "production": 
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "fileReplacements": [
                
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                
              ]
            
          
        ,
        "serve": 
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": 
            "browserTarget": "abp-zero-template:build"
          ,
          "configurations": 
            "hmr": 
              "browserTarget": "abp-zero-template:build:hmr"
            ,
            "production": 
              "browserTarget": "abp-zero-template:build:production"
            
          
        ,
        "extract-i18n": 
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": 
            "browserTarget": "abp-zero-template:build"
          
        ,
        "test": 
          "builder": "@angular-devkit/build-angular:karma",
          "options": 
            "tsConfig": "src/tsconfig.json",
            "main": "src/test.ts",
            "karmaConfig": "./karma.conf.js",
            "polyfills": "src/polyfills.ts",
            "styles": [
              "node_modules/animate.css/animate.min.css",
              "node_modules/quill/dist/quill.core.css",
              "node_modules/quill/dist/quill.snow.css",
              "node_modules/famfamfam-flags/dist/sprite/famfamfam-flags.css",
              "node_modules/primeng/resources/primeng.min.css",
              "node_modules/primeng/resources/themes/nova-light/theme.css",
              "node_modules/primeicons/primeicons.css",
              "node_modules/sweetalert2/dist/sweetalert2.css",
              "node_modules/cookieconsent/build/cookieconsent.min.css",
              "src/assets/FreezeUI/freeze-ui.min.css",

              "src/assets/primeng/file-upload/css/primeng.file-upload.css",
              "src/assets/primeng/autocomplete/css/primeng.autocomplete.css",
              "src/assets/primeng/tree/css/primeng.tree.css",
              "src/assets/primeng/context-menu/css/primeng.context-menu.css",

              "src/assets/fonts/fonts-poppins.css",
              "src/assets/fonts/fonts-roboto.css",

              "src/assets/ngx-bootstrap/bs-datepicker.css",

              "src/assets/metronic/vendors/global/vendors.bundle.css",

              "src/app/shared/core.less",
              "src/app/shared/layout/layout.less",
              "src/styles.css"
            ],
            "scripts": [
              "node_modules/@aspnet/signalr/dist/browser/signalr.min.js",
              "node_modules/localforage/dist/localforage.js",
              "node_modules/moment/min/moment-with-locales.js",
              "node_modules/moment-timezone/builds/moment-timezone-with-data.js",
              "node_modules/push.js/bin/push.js",
              "node_modules/quill/dist/quill.js",
              "node_modules/sweetalert2/dist/sweetalert2.js",
              "node_modules/cookieconsent/build/cookieconsent.min.js",
              "src/assets/FreezeUI/freeze-ui.js",

              "src/assets/abp-web-resources/abp.js",
              "src/assets/abp-web-resources/abp.sweet-alert.js",
              "src/assets/abp-web-resources/abp.notify.js",
              "src/assets/abp-web-resources/abp.freeze-ui.js",
              "node_modules/abp-web-resources/Abp/Framework/scripts/libs/abp.moment.js",

              "src/assets/metronic/vendors/global/vendors.bundle.js",
              "src/assets/metronic/common/js/scripts.bundle.js"
            ],
            "assets": [
              "src/assets",
              "src/favicon.ico",
              
                "glob": "abp.signalr-client.js",
                "input": "node_modules/abp-web-resources/Abp/Framework/scripts/libs",
                "output": "/assets/abp"
              
            ]
          
        ,
        "lint": 
          "builder": "@angular-devkit/build-angular:tslint",
          "options": 
            "tsConfig": [
              "src/tsconfig.json"
            ],
            "exclude": []
          
        
      
    ,
    "abp-zero-template-e2e": 
      "root": "",
      "sourceRoot": "",
      "projectType": "application",
      "architect": 
        "e2e": 
          "builder": "@angular-devkit/build-angular:protractor",
          "options": 
            "protractorConfig": "./protractor.conf.js",
            "devServerTarget": "abp-zero-template:serve",
            "port": 4200
          
        ,
        "lint": 
          "builder": "@angular-devkit/build-angular:tslint",
          "options": 
            "tsConfig": [
              "e2e/tsconfig.json"
            ],
            "exclude": []
          
        
      
    
  ,
  "defaultProject": "abp-zero-template",
  "schematics": 
    "@schematics/angular:component": 
      "prefix": "app",
      "styleext": "css"
    ,
    "@schematics/angular:directive": 
      "prefix": "app"
    
  

【问题讨论】:

【参考方案1】:

打开angular-config.json并删除内容,然后粘贴


  "version": 1,
  "cli": 
    "analytics": false
  


/*This solution also helps when you get the error shown below after running -> [ng new projectName] 
error = [ An unhandled exception occurred: Workspace config file cannot be
loaded: //C:\Users\username\.angular-config.json Unexpected end of the file.] */

【讨论】:

【参考方案2】:

确保angular.json 文件编码为UTF-8。

【讨论】:

以上是关于ng build :无法加载工作区配置文件的主要内容,如果未能解决你的问题,请参考以下文章

如何在“ng build --prod”之后让我的 Angular 应用程序使用编辑过的配置文件

Flume-ng禁用自动加载配置文件功能

Spring Junit单元测试无法加载配置文件

Spring Junit单元测试无法加载配置文件

java 编译成功,运行class文件出现“找不到或无法加载主类”,啥原因?

Gcloud 云构建本地组件失败并出现错误“加载配置文件时出错:cloudbuild.Build 中的未知字段“availableSecrets””