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

8aqjt8rx  于 11个月前  发布在  Angular
关注(0)|答案(6)|浏览(195)
Global Angular CLI: 7.3.8
Node v10.15.3
NPM 6.4.1
macos

字符串
我在npm start上得到这个错误
Error in ./node_modules/@angular-devkit/build-angular/src/angular-config-files/models/jit-polyfills.js模块未找到:错误:无法解析“core-js/es 7/reflect”in“/Users/XXX/projects/XXX/node_modules/@angular-devkit/build-angular/src/angular-config-files/models”错误:无法解析“core-js/es 7/reflect”in“./src/polyfills.ts模块未找到:错误:无法解析”core-js/es 7/reflect“in”/Users/XXX/projects/XXX/src“

q3aa0525

q3aa05251#

为了解决这个问题,我在tslog.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"
    ]
  }
}

字符串

eagi6jfj

eagi6jfj2#

迁移到新的Angular版本后,'core-js/es6'或'core-js/es 7'将无法工作。
您只需将import 'core-js/es/'
对于ex. import 'core-js/es6/symbol',导入'core-js/es/symbol'
这将正常工作。

cpjpxq1n

cpjpxq1n3#

只需删除路径中'es'结尾的数字,如'core-js/es/reflect'。这对我很有效。

fd3cxomn

fd3cxomn4#

我只是从我最旧的项目src/polyfills复制到新导入的项目中。这是帮助;)。

xn1cxnb4

xn1cxnb45#

"@angular/cli": "~10.1.5"项目中获取此错误:
找不到模块“code-js/es 7/reflect”或其相应的类型声明。ts(2307)
解决方法:

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

字符串

vjrehmav

vjrehmav6#

虽然这个问题是关于Angular 6到7的,但我把它当作一个错误的规范。

import 'core-js/proposals/reflect-metadata';

字符串
在polyfills.ts.
作为参考,这是我的包。json:

{
  "dependencies": {
    "@angular/animations": "^12.1.0",
    "@angular/common": "^12.1.0",
    "@angular/compiler": "^12.1.0",
    "@angular/core": "^12.1.0",
    "@angular/forms": "^12.1.0",
    "@angular/platform-browser": "^12.1.0",
    "@angular/platform-browser-dynamic": "^12.1.0",
    "@angular/router": "^12.1.0",
    "@ngrx/store": "12.5.1",
    "core-js": "3.22.4",
    "rxjs": "^6.5.4",
    "tslib": "^2.3.0",
    "zone.js": "^0.11.4"
  },
  "devDependencies": {
    "@angular-builders/jest": "^12.0.2",
    "@angular-devkit/build-angular": "^12.1.2",
    "@angular/cli": "^12.1.2",
    "@angular/compiler-cli": "^12.1.0",
    "@types/core-js": "2.5.5",
    "@types/jest": "^27.0.3",
    "@types/node": "^12.11.1",
    "jest": "^27.4.5",
    "jest-preset-angular": "^11.0.1",
    "typescript": "^4.2.2"
  }
}


. angular-json.json:

{
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": ["assets", "favicon.ico"],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "prefix": "app",
      "styles": ["styles.css"],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts"
      }
    }
  ]
}


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

相关问题