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

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?



Solution 1:[1]

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!

Solution 2:[2]

I fixed it in my project with this command:

npm install [email protected] --save

I was getting this error using [email protected].

Solution 3:[3]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ricardo Rocha
Solution 2 djangofan
Solution 3 Ahmer Ali Ahsan