Dmitry Grinko
Dmitry Grinko

Reputation: 15212

Migration from Angular 6 to Angular 7 cause error - Can't resolve 'core-js/es7/reflect'

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

I'm getting this error on npm start

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 '/Users/XXX/projects/XXX/node_modules/@angular-devkit/build-angular/src/angular-cli-files/models' ERROR in ./src/polyfills.ts Module not found: Error: Can't resolve 'core-js/es7/reflect' in '/Users/XXX/projects/XXX/src'

Upvotes: 4

Views: 9439

Answers (6)

ggorlen
ggorlen

Reputation: 57195

Although the question asks about Angular 6 to 7, I'm treating it as a canonical for the error. The import that worked for me in Angular 12 was

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

in polyfills.ts.

For reference, here's my package.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-cli.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"
      }
    }
  ]
}

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

Upvotes: 0

Pinaki
Pinaki

Reputation: 1030

Getting this error in "@angular/cli": "~10.1.5" project:

Cannot find module 'core-js/es7/reflect' or its corresponding type declarations.ts(2307)

Solution:

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

Upvotes: 0

Stanley Mohlala
Stanley Mohlala

Reputation: 7531

just remove the number at the end of 'es' in the path, like 'core-js/es/reflect'. It worked for me.

Upvotes: 2

Sergiu Siscovschi
Sergiu Siscovschi

Reputation: 1

I just copy all from my oldest project src/polyfills to the new imported. That's help ;) .

Upvotes: 0

Bhadresh Patel
Bhadresh Patel

Reputation: 2080

After Migrated to new Angular version, 'core-js/es6' or 'core-js/es7' Will not work.

You have to simply replace import 'core-js/es/'

For ex. import 'core-js/es6/symbol' to import 'core-js/es/symbol'

This will work properly.

Upvotes: 5

Dmitry Grinko
Dmitry Grinko

Reputation: 15212

To solve this issue I've added the following paths to compilerOptions in tsconfig.json

{
  "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"
    ]
  }
}

Upvotes: 18

Related Questions