./src/polyfills.ts 中的错误未找到模块:错误:无法解析“zone.js/dist/zone”

问题描述 投票:0回答:6

我有一个 Angular 8,它使用 karma/jasmine 来运行一些单元测试。我可以通过执行以下命令来运行测试

ng test
但出现以下错误:

./src/polyfills.ts 中的错误找不到模块:错误:无法解析 'zone.js/dist/zone' 中 'C:\PrjNET\Elevation3\FW .00\Project\Framework\Development\Client\ElevationJS\shell\src' 解析 'zone.js/dist/zone' 中 'C:\PrjNET\Elevation3\FW .00\Project\Framework\Development\Client\ElevationJS\shell\src'

有谁知道怎么解决吗?

angular typescript unit-testing jasmine karma-runner
6个回答
24
投票

我用这个命令在我的项目中修复了它:

npm install [email protected] --save

我使用

[email protected]
时遇到此错误。


8
投票

zone-testing
仅存在于
0.8.19
。尝试安装最新版本。使用:

npm i zone.js

以上命令适用于我的情况。


8
投票

改变

   zone.js/dist/zone

   zone.js

对于我来说,更新版本确实没有工作。

我检查了使用“ng new project”创建的项目,在“polyfills.ts”文件中,它是“zone.js”而不是“zone.js/dist/zone”

所以我将其更改为“zone.js”,它开始工作了。

错误: enter image description here

正确: enter image description here


4
投票

我发现我在

tsconfig.spec.json
上的测试配置是错误的。所以我改变了:

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

对此:

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

错误消失了!


1
投票

基本上导入有问题。所以我在node_modules/core-js中查找reflect文件夹,然后在polyfills.js中复制粘贴相同的路径。 成功了。

来自

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

import "core-js/es/reflect";

0
投票

就我而言,我从 Angular 7 迁移到 Angular 18,遇到了这个问题。所以为了解决这个问题,我去了

polyfills.ts
文件并更改了: import
'zone.js/dist/zone'
来导入
'zone.js'

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.