从Angular 6迁移到Angular 7会导致错误 - 无法解析'core-js/es7/reflect'。

4
Global Angular CLI: 7.3.8
Node v10.15.3
NPM 6.4.1
macos

在运行npm start时出现以下错误:

错误信息: ./node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/jit-polyfills.js 模块未找到:错误:无法解析 'core-js/es7/reflect' 在 '/Users/XXX/projects/XXX/node_modules/@angular-devkit/build-angular/src/angular-cli-files/models'中 错误信息2:./src/polyfills.ts 模块未找到:错误:无法解析 'core-js/es7/reflect' 在 '/Users/XXX/projects/XXX/src'中


对于正在升级到Angular 8或更高版本的项目,您可以将core.js作为依赖项进行卸载:https://dev59.com/QVMI5IYBdhLWcg3wJ4Oz - John Gilmer
@JohnGilmer 谢谢您的评论。请随意将其添加为答案。这将会非常有帮助。 - Dmitry Grinko
5个回答

18
为了解决这个问题,我已经在tsconfig.json的compilerOptions中添加了以下路径。
{
  "compileOnSave": false,
  "compilerOptions": {
    "paths": {
      "core-js/es7/reflect": [
        "node_modules/core-js/proposals/reflect-metadata",
      ],
      "core-js/es6/*": ["node_modules/core-js/es"]
    },
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

2
你真是个救命恩人。这正是我所需要的。 - 今際のアリス

5

迁移到新版Angular后,'core-js/es6'或'core-js/es7'将不再起作用。

你只需要简单地替换导入 'core-js/es/'

例如:将 import 'core-js/es6/symbol' 替换为 import 'core-js/es/symbol'

这样就能正常工作了。


2

只需在路径中删除'es'后面的数字,例如'core-js/es/reflect'。我尝试过,这个方法可行。


0

我只是从我的最早的项目 src/polyfills 中复制了所有内容到新导入的项目中。这很有帮助 ;)。


0

"@angular/cli": "~10.1.5" 项目中遇到以下错误:

无法找到模块 'core-js/es7/reflect' 或其对应的类型声明.ts(2307)

解决方案:

import * as Reflect from 'core-js/es';

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接