91

After updating to Angular 7.3.6, I get the following error on ng serve:

enter image description here

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'

Here is my package.json dependecies:

{
  "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"
  }
}
Gregor Albert
  • 819
  • 1
  • 11
  • 23
Bronsii
  • 1,023
  • 1
  • 7
  • 10
  • Did you clean your node_modules, node cache and try `npm install` from fresh before running `ng serve`? – nircraft Mar 28 '19 at 13:36
  • yes I did, but the same error keeps on appearing and I don't know what else to do. Do you have any other suggestions? – Bronsii Mar 28 '19 at 13:46
  • 1
    You should install `core-js@2.5.x` – nircraft Mar 28 '19 at 13:48
  • yes it worked! thank you – Bronsii Mar 28 '19 at 13:57
  • Adding it as answer, please accept. – nircraft Mar 28 '19 at 14:01
  • Did face the same issue! but thanks to @nircraft, it worked for me too. – Abhijit Srivastava Apr 01 '19 at 20:13
  • In core-js@3 es7 path removed. Use, for example, core-js/proposals/reflect-metadata. Here is the github link [link](https://github.com/zloirock/core-js/issues/412) – Himalaya Garg Jun 16 '19 at 15:28
  • 2
    As of July 3, 2019, using Angular-CLI 8.0.6 and Angular 8.1.0, the above error will no longer happen on **NEWLY GENERATED** Angular apps because the offending import is no longer generated in /src/polyfills.ts. For older Angular applications, I suggest generating a temporary Angular app via: ``` ng new aaTemplate --skip-tests --skip-install ``` Compare the newly generated aaTemplate\src\polyfills.ts with your older application's polyfills.ts and you'll see what I'm talking about. – user3785010 Jul 03 '19 at 11:57
  • For projects being upgraded to Angular 8 or later, you can uninstall core.js as a dependency: https://stackoverflow.com/questions/56892243/is-not-core-js-needed-anymore – John Gilmer Oct 23 '20 at 07:18

12 Answers12

110

You should install core-js@2.5.x that contains this file/module. See preset-env docs.

Also note, however, that this is a very old version with known issues and is unsupported. From the module itself:

"core-js@<3.23.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Some versions have web compatibility issues. Please, upgrade your dependencies to the actual version of core-js."

Please consider using one of the better solutions below.

karora
  • 1,223
  • 14
  • 31
nircraft
  • 8,242
  • 5
  • 30
  • 46
  • 42
    `npm install --save core-js@^2.5.0`, worked for me, thanks – Jeremy Thille Mar 29 '19 at 12:29
  • 1
    Works for me too !! – Abhijit Srivastava Apr 01 '19 at 20:12
  • 1
    I decreased the version in package.json to "core-js": "^2.5.4", and run yarn, that worked for me. – Sami-L Apr 23 '19 at 13:18
  • 1
    Worked !! Thank you – SudarP Apr 26 '19 at 00:15
  • 2
    Confirmed that npm install --save core-js@^2.5.0 worked for me as well – iamtravisw Dec 02 '20 at 04:14
  • For Angular 13, finally I succeeded to get 'ng test' working with ==> npm install --save core-js@^3.3 – tm1701 Dec 17 '21 at 19:27
  • This might have been OK back in 2019 but seems *very* bad advice now since "core-js@<3.23.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Some versions have web compatibility issues. Please, upgrade your dependencies to the actual version of core-js." – karora Sep 01 '22 at 21:34
40

If you'd like to use version 3.0, you can add the a path to your tsconfig.json file.

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

Note: You need check the node_modules/core-js/proposals/reflect-metadata relative path and correct it if needed in your project structure.

Medeni Baykal
  • 4,223
  • 1
  • 28
  • 36
  • Hi I've seen this also on gh erro reports but haven't managed to make it work, just got the same Module not found: Error: Can't resolve 'core-js/es7/reflect' error....does yours works? – Camilo Casadiego Apr 30 '19 at 02:43
  • 6
    for me works this : "paths": { "core-js/es7/reflect": ["../node_modules/core-js/proposals/reflect-metadata"], "core-js/es6/*": ["../node_modules/core-js/es/*"] }, – mrapi May 29 '19 at 06:35
  • Ran `ncu` which updated all my packages to the latest then used the above and it worked for me. –  Aug 04 '19 at 20:08
  • I had to add "baseUrl": "." to make it works. Thanks mrapi amd Medeni baykal – Cedric Arnould Aug 09 '19 at 18:22
33

For 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).

I've resolved by:

  • removing core-js from my package.json dependencies
  • removing import 'core-js/es7/reflect'; from my test.ts
Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252
  • 4
    For a similar issue this worked, but I needed to remove the references to `core-js/es{6,7}/reflect` from my `polyfills.ts` – beetstra Jan 07 '20 at 08:51
28

The version 3.0 of core-js has some breaking changes:

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

You should find a line similar to this in one of your files: import "core-js/es7/reflect";

Change it to this: import "core-js/proposals/reflect-metadata";

pbristow
  • 1,997
  • 4
  • 26
  • 46
Joffrey Hernandez
  • 1,809
  • 3
  • 21
  • 39
24

In my case , i just changed -->

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

to

import 'core-js/es/reflect';

and it worked..

Brijesh Ray
  • 779
  • 8
  • 15
6

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

For EX: import 'core-js/es7/reflect'; was changed to import 'core-js/es/reflect';

nircraft
  • 8,242
  • 5
  • 30
  • 46
David1er
  • 106
  • 1
  • 2
  • Can you explain a bit more? – Dieter Meemken Nov 06 '19 at 16:32
  • Hi Dieter, the problem is that your are looking for the "reflect" module in a wrong folder. 1- open the "polyfills.ts" in your src folder. 2 - find the line where there is a line like "import 'core-js/es7/reflect';". Here it is 7 for me, it can be something else in your case. 3- Remove the 7 like that: "import 'core-js/es/reflect';" and it should works. – David1er Nov 07 '19 at 15:49
4

The tsconfig.json solution from @medeni works, but don't copy the path before checking it by yourself. For me it had to be:

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

the ../ is because tsconfig.json is extended by src/tsconfig.app.json or src/tsconfig.spec.json, so /src is the working directory.

note: I am using Angular CLI 8.1.0

avdm
  • 71
  • 3
4

I fixed it by adding this import: import 'core-js/proposals/reflect-metadata'; in my polyfills.ts file

/** 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';

Make sure you have the latest core-js update and everything should work.

Raul Bermudez
  • 51
  • 1
  • 4
4

For angular latest version (currently -> 9) ,

compatible core-js version is "core-js": "~2.5.0".

step 1) Delete Node modules.

step 2) In package.json file, write "core-js": "~2.5.0" in dependecies object.

step 3) npm install

then It will work perfectly fine

1

I found possible answer. You have core-js version 3.0, and this version doesn't have separate folders for ES6 and ES7; that's why the application cannot find correct paths.

To resolve this error, you can downgrade the core-js version to 2.5.7. This version produces correct catalogs structure, with separate ES6 and ES7 folders.

To downgrade the version, simply run:

npm i -S core-js@2.5.7
nircraft
  • 8,242
  • 5
  • 30
  • 46
Sumanth
  • 477
  • 1
  • 6
  • 11
1

I solved the problem by removing the number at the end of "es" in the path as in response to Module not found: Error: Can't resolve 'core-js/es7/reflect

0

For Angular 7

From package.json remove:

"core-js": "3.6.5",

because it is part of the Angular package.

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Umbrella Brothers
  • 327
  • 1
  • 5
  • 15