【问题标题】:Angular 6: Failed to compile: Can't resolve 'stream' in ./node_modules/hash-baseAngular 6:编译失败:无法解析 ./node_modules/hash-base 中的“流”
【发布时间】:2018-10-27 10:47:57
【问题描述】:

我将三个 Angular 5 应用程序更新为 Angular 6,但最后一个给我带来了麻烦。 每次我尝试编译(服务/构建、jit/aot、dev/prod)时,我都会收到这个错误:

ERROR in ./node_modules/hash-base/index.js
Module not found: Error: Can't resolve 'stream' in '[...]/node_modules/hash-base'
ℹ 「wdm」: Failed to compile.

hash-base 似乎是一个 webpack 依赖:

@eagle5/editor-app@0.2.6
└─┬ @angular-devkit/build-angular@0.6.1
  └─┬ webpack@4.6.0
    └─┬ node-libs-browser@2.1.0
      └─┬ crypto-browserify@3.12.0
        └─┬ create-hash@1.2.0
          ├─┬ md5.js@1.3.4
          │ └── hash-base@3.0.4 
          └─┬ ripemd160@2.0.2
            └── hash-base@3.0.4  deduped

这个应用程序的一些特点是我在 app 文件夹旁边的 ./src 中有一个框架,我将一个 git 存储库安装为 npm 包,并在我的 tsconfig.json 中导入一些 typescript 定义:

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "baseUrl": "src",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ],
    "paths": {
      "@nettrek/*": [
        "./@nettrek/*"
      ]
    }
  },
  "include": [
    "./src/**/*",
    "./node_modules/@eagle5/player-core/dto/**/*"
  ],
  "angularCompilerOptions": {
    "preserveWhitespaces": false
  }
}

我尝试安装thisstream npm 包,但是这个版本没有 hash-base 使用的 stream.Transform 扩展。

我试过节点 8.11.1 和 10.1.0

【问题讨论】:

  • 试试npm -ls。你有没有收到包裹丢失之类的错误?
  • no npm ls 没有给我任何错误

标签: angular typescript webpack angular6


【解决方案1】:

好的,我找到了错误的原因。我们自己在一个类中使用了 md5.js,却忘了把它放在我们的 package.json 中,因为它已经安装好了。 Md5.js 当然需要 Angular 6 不再支持的浏览器 shims 中的节点。

【讨论】:

  • 如何添加md5.js?
  • 它确实似乎是 md5 包。我改用这个 github.com/cotag/ts-md5
  • 我也使用ts-md5 包。像这样导入它import { Md5 } from 'ts-md5/dist/md5';
【解决方案2】:

尝试将“流”添加到您的项目中

npm install stream --save

【讨论】:

  • 虽然这可能会解决问题,但它也可能会破坏某些内容,因为 stream 引用了 node.js 构造,而该构造在浏览器中不可用。
猜你喜欢
  • 2020-01-12
  • 2019-02-13
  • 1970-01-01
  • 2020-08-30
  • 2020-10-30
  • 2018-10-19
  • 2021-01-12
  • 1970-01-01
  • 2019-05-23
相关资源
最近更新 更多