ERROR in ./src/polyfills.ts Module not found: Error: Can't resolve 'zone.js/dist/zone'
Asked Answered
M

6

11

I have an angular 8, that uses karma/jasmine to run some unit tests. I can run tests by executing the following command ng test but I'm getting the following error:

ERROR in ./src/polyfills.ts Module not found: Error: Can't resolve 'zone.js/dist/zone' in 'C:\PrjNET\Elevation3\FW\4.00\Project\Framework\Development\Client\ElevationJS\shell\src' resolve 'zone.js/dist/zone' in 'C:\PrjNET\Elevation3\FW\4.00\Project\Framework\Development\Client\ElevationJS\shell\src'

Any one know how to solve it?

Morita answered 12/7, 2019 at 9:0 Comment(6)
Try npm i zone.jsKati
@GopeshSharma I have installed and still does not workMorita
In your test file are you importing zone.js/dist/zone?. If yes try changing it to import 'zone.js/dist/zone-testing'; Though zone-testing only exists from zone.js 0.8.19Kati
@GopeshSharma Yes I know, but I'm importing well (import 'zone.js/dist/zone-testing'). Also, I'm using the version 0.8.29Morita
I had the same issue today and I checked the above answers. Although a simple npm install did the trick. My latest pull had a new package that I hadn't added to my local repository and the solution was a simple npm install.Glasswort
This is a different problem but I stumbled onto this question. Maybe someone will profit from it: Update zone.js to something higher than 0.11 and change the zone import in the polyfill.ts to import 'zone.js';Trespass
M
4

I found out that my test configurations on tsconfig.spec.json were wrong. So I change from this:

{
  "extends": "./tsconfig.es5.json",
  "compilerOptions": {
    "outDir": "../out-tsc/spec",
    "module": "commonjs",
    "target": "es6",
    "baseUrl": "",
    "types": [
      "jest",
      "node"
    ]
  },
  "files": [
    "test.ts",
    "polyfills.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts"
  ]
}

to this:

{
  "extends": "./tsconfig.es5.json",
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "outDir": "../out-tsc/spec",
    "module": "commonjs",
    "types": [
      "jasmine",
      "node"
    ]
  },
  "files": [
    "test.ts",
    "polyfills.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts"
  ]
}

And the error disapeared!

Morita answered 12/7, 2019 at 10:34 Comment(2)
This answer doesn't help much without knowing your effective combined configuraiton.Mastership
Getting the following error 'Error: Module not found: Error: Can't resolve '/home/anshuman/Documents/repos/jovian-genai-hackathon/UI/zone.js' in '/home/anshuman/Documents/repos/jovian-genai-hackathon/UI''Vaulting
M
24

I fixed it in my project with this command:

npm install [email protected] --save

I was getting this error using [email protected].

Mastership answered 23/11, 2020 at 19:43 Comment(0)
W
10

Change

   zone.js/dist/zone

to

   zone.js

As for me, updating the version did NOT work.

I checked the project created with "ng new project", and in the "polyfills.ts" file, it was "zone.js" instead of "zone.js/dist/zone".

So I changed it to "zone.js", and it started to work.

Incorrect: enter image description here

Correct: enter image description here

Witticism answered 11/7, 2022 at 7:13 Comment(0)
G
8

zone-testing only exists from 0.8.19. Try to install the latest version. using:

npm i zone.js

above command worked in my case.

Guyton answered 8/5, 2022 at 4:28 Comment(2)
This also helps when running the app and you get this error.Arsenate
solved my problem, tyIndecorous
M
4

I found out that my test configurations on tsconfig.spec.json were wrong. So I change from this:

{
  "extends": "./tsconfig.es5.json",
  "compilerOptions": {
    "outDir": "../out-tsc/spec",
    "module": "commonjs",
    "target": "es6",
    "baseUrl": "",
    "types": [
      "jest",
      "node"
    ]
  },
  "files": [
    "test.ts",
    "polyfills.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts"
  ]
}

to this:

{
  "extends": "./tsconfig.es5.json",
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "outDir": "../out-tsc/spec",
    "module": "commonjs",
    "types": [
      "jasmine",
      "node"
    ]
  },
  "files": [
    "test.ts",
    "polyfills.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts"
  ]
}

And the error disapeared!

Morita answered 12/7, 2019 at 10:34 Comment(2)
This answer doesn't help much without knowing your effective combined configuraiton.Mastership
Getting the following error 'Error: Module not found: Error: Can't resolve '/home/anshuman/Documents/repos/jovian-genai-hackathon/UI/zone.js' in '/home/anshuman/Documents/repos/jovian-genai-hackathon/UI''Vaulting
Z
1

Basically there is an issue with imports.So I looked for reflect folder inside node_modules/core-js and I copy pasted same path in polyfills.js. It worked.

from

import "core-js/es/reflect";
import "core-js/es7/reflect";

to

import "core-js/es/reflect";
Zuber answered 18/5, 2023 at 10:53 Comment(2)
This can't work if you are doing a CI/CD setup though, right?Vaulting
@AnshumanKumar I am not sure.I don't have devops skills.But my code worked in production.Zuber
U
0

In my case I was migrating from Angular 7 to Angular 18 and I had this problems. So to solve I went fo polyfills.ts file and I changed : import 'zone.js/dist/zone' to import 'zone.js'

enter image description here

Unemployment answered 25/7 at 8:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.