Error: Can't resolve 'core-js/es7/reflect' in '\node_modules\@angular-devkit\build-angular\src\angular-cli-files\models

Error: Can't resolve 'core-js/es7/reflect' in '\node_modules\@angular-devkit\build-angular\src\angular-cli-files\models

更新到 Angular 7.3.6 后,我在 ng serve 上收到以下错误:

ERROR in ./node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/jit-polyfills.js Module not found: Error: Can't resolve 'core-js/es7/reflect' in '\node_modules@angular-devkit\build-angular\src\angular-cli-files\models'

这是我的 package.json 亲属:

{
  "name": "frontend",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build --prod",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^7.2.11",
    "@angular/cdk": "^7.3.6",
    "@angular/common": "^7.2.11",
    "@angular/compiler": "^7.2.11",
    "@angular/core": "^7.2.11",
    "@angular/flex-layout": "^7.0.0-beta.24",
    "@angular/forms": "^7.2.11",
    "@angular/http": "^7.2.11",
    "@angular/material": "^7.3.6",
    "@angular/platform-browser": "^7.2.11",
    "@angular/platform-browser-dynamic": "^7.2.11",
    "@angular/router": "^7.2.11",
    "adm-zip": "^0.4.13",
    "core-js": "^2.6.5",
    "hammerjs": "^2.0.8",
    "rxjs": "^6.4.0",
    "rxjs-compat": "^6.0.0-rc.0",
    "tslib": "^1.9.3",
    "zone.js": "^0.9.0"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.13.7",
    "@angular/cli": "^7.3.7",
    "@angular/compiler-cli": "^7.2.11",
    "@angular/language-service": "^7.2.11",
    "@types/jasmine": "~3.3.12",
    "@types/jasminewd2": "~2.0.6",
    "@types/node": "~11.12.0",
    "codelyzer": "^5.0.0",
    "jasmine-core": "~3.3.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "^4.0.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "^2.0.5",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "^6.0.0",
    "ts-node": "~8.0.3",
    "tslint": "~5.14.0",
    "typescript": "3.1.6"
  }
}

您应该安装包含此 file/module 的 core-js@2.5.x。参见 preset-env docs

core-js 3.0 版本有一些重大变化:

https://github.com/zloirock/core-js/blob/master/docs/2019-03-19-core-js-3-babel-and-a-look-into-the-future.md

您应该在您的一个文件中找到与此类似的一行:import "core-js/es7/reflect";

改成这样:import "core-js/proposals/reflect-metadata";

如果您想使用 3.0 版,可以将路径添加到 tsconfig.json 文件。

{
    "compilerOptions": {
        ...
        "paths": {
            "core-js/es7/reflect": [
                "node_modules/core-js/proposals/reflect-metadata"
            ]
        }
    }
}

注意:您需要检查 node_modules/core-js/proposals/reflect-metadata 相对路径并根据需要在您的项目结构中更正它。

我找到了可能的答案。您有 core-js 版本 3.0,并且此版本没有针对 ES6 和 ES7 的单独文件夹;这就是应用程序无法找到正确路径的原因。

要解决此错误,您可以将 core-js 版本降级到 2.5.7。此版本生成正确的目录结构,具有单独的 ES6 和 ES7 文件夹。

要降级版本,只需运行:

npm i -S core-js@2.5.7

的tsconfig.json解决方案有效,但在自己检查之前不要复制路径。对我来说必须是:

"paths": {
  "core-js/es7/reflect": ["../node_modules/@angular-devkit/build-angular/node_modules/core-js/proposals/reflect-metadata"]
}

../是因为tsconfig.json被src/tsconfig.app.json或src/tsconfig.spec.json扩展了,所以/src是工作目录.

注意:我正在使用 Angular CLI 8.1.0

我通过在我的 polyfills.ts 文件中添加此导入来修复它:import 'core-js/proposals/reflect-metadata';

/** Evergreen browsers require these. **/
// Used for reflect-metadata in JIT. If you use AOT (and only Angular decorators), you can remove.
import 'core-js/proposals/reflect-metadata';

确保您有最新的 core-js 更新并且一切正常。

对于 Angular 8 (source):

Angular CLI 8.0+ manages the required Angular polyfills directly and projects will not require a direct dependency on core-js (assuming an application does not manually include additional core-js polyfills).

我已经解决了:

  • 正在从我的 package.json 依赖项中删除 core-js
  • 正在从我的 test.ts
  • 中删除 import 'core-js/es7/reflect';

就我而言,我只是更改了 -->

import 'core-js/es6/reflect';
import 'core-js/es7/reflect';

import 'core-js/es/reflect';

成功了..

删除路径中 'es' 末尾的数字,如 'core-js/es/reflect' 对我也有效。

对于 EX:import 'core-js/es7/reflect'; 已更改为 import 'core-js/es/reflect';

对于 angular 最新版本(当前 -> 9),

兼容的 core-js 版本是 "core-js": "~2.5.0".

步骤 1) 删除节点模块。

步骤 2) 在 package.json 文件中,在依赖对象中写入 "core-js": "~2.5.0"。

步骤 3) npm 安装

那么它将完美地工作

为Angular7

package.json 删除:

"core-js": "3.6.5",

因为它是 Angular 包的一部分。

我通过删除路径中“es”末尾的数字解决了这个问题,作为对未找到模块的响应:错误:无法解析'core-js/es7/reflect