开发者问题收集

TypeError:对象原型只能是一个对象或为空:未定义 Angular 8

2019-08-08
101218

每当我尝试启动我的 Web 应用程序时,我都会收到此类型错误。无法弄清楚原因。

我在更新一些软件包后开始遇到此问题。我无法弄清楚哪一个是问题所在,也无法弄清楚错误指向的代码为什么会出错,因为它是我的 Angular 8 依赖项中的代码。我甚至不确定错误到底在告诉我什么。 我尝试更新所有依赖项。我查看了几乎所有类似的问题解决方案,但都没有奏效。我不知道这是编译器错误、typescript 还是依赖项错误。

错误

[error] TypeError: Object prototype may only be an Object or null: undefined
    at setPrototypeOf (<anonymous>)
    at Object.__extends (C:\Users\leahb\Desktop\NH\NH-Senior-Project\node_modules\tslib\tslib.js:65:9)
    at C:\Users\leahb\Desktop\NH\NH-Senior-Project\node_modules\@angular\compiler-cli\src\ngtsc\indexer\src\template.js:115:17

tslib.js at line 65
    (function (exporter) {
        var extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };

        __extends = function (d, b) { 
            extendStatics(d, b); *//saying the error is coming from here*
            function __() { this.constructor = d; }
            d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
        };

PACKAGE.JSON

{
  "name": "nh",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "heroku-postbuild": "ng build --prod"
  },
  "keywords": [
    "heroku"
  ],
  "private": true,
  "dependencies": {
    "@agm/core": "^1.0.0-beta.6",
    "@angular-devkit/build-angular": "^0.13.0",
    "@angular/animations": "~8.1.2",
    "@angular/common": "~8.1.2",
    "@angular/core": "~8.1.2",
    "@angular/forms": "~8.1.2",
    "@angular/platform-browser": "~8.1.2",
    "@angular/platform-browser-dynamic": "~8.1.2",
    "@angular/router": "~8.1.2",
    "@types/googlemaps": "^3.37.0",
    "core-js": "^2.5.4",
    "font-awesome": "^4.7.0",
    "hammerjs": "^2.0.8",
    "ngx-gallery": "^5.10.0",
    "rxjs": "~6.5.2",
    "tslib": "^1.9.0",
    "zone.js": "^0.9.1"
  },
  "devDependencies": {
    "@angular/cli": "^7.3.9",
    "@angular/compiler": "^7.2.15",
    "@angular/compiler-cli": "^8.1.2",
    "@angular/language-service": "~8.1.2",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.5.0",
    "enhanced-resolve": "^3.3.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "^3.4.5"
  },
  "engines": {
    "node": "10.16.0",
    "npm": "6.9.0"
  }
}

我期望应用程序能够编译、构建和启动。每当我尝试 ng serve、ng test 或 ng build 时,我都会收到此错误。

3个回答

在将 CLI 全局更新到版本 8 后,我的 angular@7 项目也遇到了同样的问题,通过在项目目录中运行以下命令将项目中的 angular 升级到最新版本解决了该问题:

ng update @angular/cli @angular/core

您还可以使用 allow-dirty 标志绕过 repo 检查:

ng update @angular/cli @angular/core --allow-dirty

此外,升级时还要注意此处提到的其他事项: https://update.angular.io/#7.0:8.0

Sinandro
2019-08-24

使用 ng test 运行测试时,我在 Angular 应用中遇到了同样的错误。

就我而言,问题的原因是 循环依赖

我有两个类属于某个 index.ts ,但其中一个类使用相同的索引导入了另一个类。这导致了循环依赖,因为结果 index.ts 被多次导入。

一旦删除循环依赖,一切都会正常工作。

我的灵感来自: https://stackoverflow.com/a/53123468/3497671

Francesco Borzi
2020-01-17

使用标志 allow-dirty 允许您绕过 repo 检查。

ng update @angular/cli @angular/core --allow-dirty
Hugo Ramirez
2019-09-03