簡體   English   中英

Angular 7構建錯誤-位置0的JSON中的意外令牌

[英]Angular 7 Build Error - Unexpected token in JSON at position 0

僅在構建時出錯,運行應用程序正常。

  • 錯誤:位置 0 的 JSON 中出現意外的令牌(Windows / Linux 相同的錯誤)
  • 命令:ng build --prod=true --output-hashing=all --output-path /app/dist/publish/
  • 角 7.2.2
  • 節點 16
  • NPM 8.5.5
   chunk {1090} 1090.a14a5ab24aacd3dd1d2e.js () 1.54 kB  [rendered]
    chunk {1091} 1091.8c3b264713f38e91e794.js () 2.19 kB  [rendered]
    chunk {scripts} scripts.90bded452362bf92d7cb.js (scripts) 2.19 MB [entry] [rendered]
    Unexpected token  in JSON at position 0
    SyntaxError: Unexpected token  in JSON at position 0
        at JSON.parse (<anonymous>)
        at MapSubscriber.project (/app/node_modules/@angular-devkit/build-angular/src/angular-cli-files/utilities/service-worker/index.js:96:41)
        at MapSubscriber._next (/app/node_modules/@angular-devkit/build-angular/node_modules/rxjs/internal/operators/map.js:49:35)
        at MapSubscriber.Subscriber.next (/app/node_modules/@angular-devkit/build-angular/node_modules/rxjs/internal/Subscriber.js:67:18)
        at SafeSubscriber.__tryOrUnsub (/app/node_modules/@angular/cli/node_modules/rxjs/internal/Subscriber.js:209:16)
        at SafeSubscriber.next (/app/node_modules/@angular/cli/node_modules/rxjs/internal/Subscriber.js:147:22)
        at Subscriber._next (/app/node_modules/@angular/cli/node_modules/rxjs/internal/Subscriber.js:90:26)
        at Subscriber.next (/app/node_modules/@angular/cli/node_modules/rxjs/internal/Subscriber.js:67:18)
        at SafeSubscriber.__tryOrUnsub (/app/node_modules/@angular-devkit/build-angular/node_modules/rxjs/internal/Subscriber.js:209:16)
        at SafeSubscriber.next (/app/node_modules/@angular-devkit/build-angular/node_modules/rxjs/internal/Subscriber.js:147:22)
        at SwitchMapSubscriber.notifyNext (/app/node_modules/@angular-devkit/build-angular/node_modules/rxjs/internal/operators/switchMap.js:86:26)
        at InnerSubscriber._next (/app/node_modules/@angular-devkit/build-angular/node_modules/rxjs/internal/InnerSubscriber.js:28:21)
        at InnerSubscriber.Subscriber.next (/app/node_modules/@angular-devkit/build-angular/node_modules/rxjs/internal/Subscriber.js:67:18)
        at SafeSubscriber.__tryOrUnsub (/app/node_modules/@angular/cli/node_modules/rxjs/internal/Subscriber.js:209:16)
        at SafeSubscriber.next (/app/node_modules/@angular/cli/node_modules/rxjs/internal/Subscriber.js:147:22)
        at Subscriber._next (/app/node_modules/@angular/cli/node_modules/rxjs/internal/Subscriber.js:90:26)
    The command '/bin/sh -c $(npm bin)/ng build --prod=true --output-hashing=all --output-path /app/dist/publish/' returned a non-zero code: 1
    ##[error]The command '/bin/sh -c $(npm bin)/ng build --prod=true --output-hashing=all --output-path /app/dist/publish/' returned a non-zero code: 1

我已經將 package.json / angular.json package-lock.json 更改為 UTF8。

我的 tsconfig.json

{
  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es6", "dom" ],
    "mapRoot": "./",
    "module": "esnext",
    "skipLibCheck": true,
    "moduleResolution": "node",
    "outDir": "../dist/out-tsc",
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [
      "../node_modules/@types"
    ],
    "baseUrl": ".",
    "paths": {
      "@eaf/*": [ "./assets/lib/eaf-ng2-module/src/*" ],
      "@app/*": [ "./app/*" ],
      "@shared/*": [ "./shared/*" ],
      "@node_modules/*": [ "../node_modules/*" ],
      "@angular/*": [ "../node_modules/@angular/*" ],
      "@metronic/*": [ "./assets/lib/metronic/*" ]
    }
  },
  "exclude": [ "node_modules" ],
  "angularCompilerOptions": {
    "annotateForClosureCompiler": false,
    "disableTypeScriptVersionCheck": true,
    "enableI18nLegacyMessageIdFormat": true,
    "trace": true
  },
}

我的 angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "gol-encantometro-ui": {
      "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",
            "serviceWorker": true,
            "ngswConfigPath": "ngsw-config.json",
            "aot": true,
            "assets": [
              "src/assets",
              "src/web.config",
              {
                "glob": "eaf.signalr-client.js",
                "input": "src/assets/lib/eaf-web-resources/Eaf/Framework/scripts/libs",
                "output": "/assets/eaf"
              },
              "src/manifest.json"
            ],
            "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",
              "node_modules/angular-calendar/css/angular-calendar.css",
              "src/assets/lib/freezeUI/freeze-ui.min.css",
              "src/assets/lib/primeng/file-upload/css/primeng.file-upload.css",
              "src/assets/lib/primeng/autocomplete/css/primeng.autocomplete.css",
              "src/assets/lib/primeng/tree/css/primeng.tree.css",
              "src/assets/lib/primeng/context-menu/css/primeng.context-menu.css",
              "src/assets/common/fonts/fonts-gol.css",
              "src/assets/lib/ngx-bootstrap/bs-datepicker.css",
              "src/assets/lib/metronic/assets/vendors/base/vendors.bundle.css",
              "src/assets/common/styles/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",
              "node_modules/html2canvas/dist/html2canvas.min.js",
              "node_modules/jspdf/dist/jspdf.min.js",
              "src/assets/lib/freezeUI/freeze-ui.js",
              "src/assets/lib/eaf-web-resources/eaf.js",
              "src/assets/lib/eaf-web-resources/eaf.sweet-alert.js",
              "src/assets/lib/eaf-web-resources/eaf.notify.js",
              "src/assets/lib/eaf-web-resources/eaf.freeze-ui.js",
              "src/assets/lib/eaf-web-resources/Eaf/Framework/scripts/libs/eaf.moment.js",
              "src/assets/lib/metronic/assets/vendors/base/vendors.bundle.js",
              "src/assets/lib/metronic/assets/vendors/base/scripts.bundle.js",
              "src/assets/lib/eaf-web-resources/jquery.js",
              "src/assets/lib/eaf-web-resources/functions.js"
            ]
          },
          "configurations": {
            "production": {
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "aot": true,
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.build.ts"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "gol-encantometro-ui:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "gol-encantometro-ui:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "gol-encantometro-ui:build"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": ["src/tsconfig.json"],
            "exclude": []
          }
        }
      }
    }
  },
  "defaultProject": "gol-encantometro-ui",
  "schematics": {
    "@schematics/angular:component": {
      "prefix": "app",
      "styleext": "css"
    },
    "@schematics/angular:directive": {
      "prefix": "app"
    }
  }
}

謝謝克里斯·漢密爾頓

我打開文件時出現錯誤/app/node_modules/@angular-devkit/build-angular/src/angular-cli-files/utilities/service-worker/index.js:96:41

並放置一個console.log以獲取json中的錯誤文件

錯誤出現在文件ngsw-config.json是帶有 BOM 的 UTF8 中,我保存了一個沒有 BOM 的文件,這可以正常工作!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM