找不到模块:错误:无法解析“ crypto”

Ill*_*lep 2 javascript node.js npm angular nem

我在运行时收到以下错误列表ng serve。

我的包JSON如下:

{   "name": "ProName",   "version": "0.0.0",   "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"   },   "private": true,   "dependencies": {
    "@angular-devkit/build-angular": "~0.12.0",
    "@angular/animations": "5.2.10",
    "@angular/common": "5.2.10",
    "@angular/compiler": "5.2.10",
    "@angular/compiler-cli": "5.2.10",
    "@angular/core": "5.2.10",
    "@angular/forms": "5.2.10",
    "@angular/platform-browser": "5.2.10",
    "@angular/platform-browser-dynamic": "5.2.10",
    "@angular/router": "5.2.10",
    "@types/dotenv": "^4.0.3",
    "@types/errorhandler": "0.0.32",
    "@types/express": "^4.16.0",
    "@types/node": "^10.5.1",
    "apostille-library": "^7.1.0",
    "core-js": "^2.5.4",
    "dotenv": "^6.0.0",
    "errorhandler": "^1.5.0",
    "express": "^4.16.0",
    "nem2-sdk": "^0.9.7",
    "rxjs": "~6.3.3",
    "stream": "0.0.2",
    "tslib": "^1.9.0",
    "typescript": "^2.9.2",
    "zone.js": "~0.8.26"   } }
Run Code Online (Sandbox Code Playgroud)

我得到的错误:

./node_modules/aws-sign2/index.js中的错误找不到模块:错误:无法解析“ / Users / MYPC / Documents / Myproj / ProName / node_modules / aws-sign2”中的“ crypto”。./node_modules中的错误/aws4/aws4.js找不到模块:错误:无法解析'/ Users / MYPC / Documents / Myproj / ProName / node_modules / aws4'中的'crypto'错误。/node_modules/ecc-jsbn/index.js模块中找不到:错误:无法在'/ Users / MYPC / Documents / Myproj / ProName / node_modules / ecc-jsbn'中解析'crypto'在./node_modules/http-signature/lib/verify.js中找不到错误模块:错误:无法在'/ Users / MYPC / Documents / Myproj / ProName / node_modules / http-signature / lib'中解析'crypto'在./node_modules/http-signature/lib/signer.js中的错误未找到模块:错误:无法解析“./node_modules/nem-sdk/build/external/nacl-fast.js中的/ Users / MYPC / Documents / Myproj / ProName / node_modules / http-signature / lib'错误找不到模块:错误:无法解析'crypto在'/ Users / MYPC / Documents / Myproj / ProName / node_modules / nem-sdk / build / external'中的错误在./node_modules/nem-sdk/node_modules/aws-sign2/index.js中

小智 35

在该项目下的tsconfig.ts文件中添加此设置可解决此警告

"compilerOptions": {
"baseUrl": "./",
"paths": {
  "crypto": [
    "../../node_modules/crypto-js"
  ]
}
Run Code Online (Sandbox Code Playgroud)

  • finame 应该是 tsconfig.json (2认同)

lem*_*ing 20

我想我会扩展塔里克·艾哈迈德 (Tarique Ahmed) 在他的回答中所写的内容。

我使用的 npm 模块在代码中包含以下行:

const crypto = require('crypto');
Run Code Online (Sandbox Code Playgroud)

我无法添加:

"browser": {
  "crypto": false
}
Run Code Online (Sandbox Code Playgroud)

到 package.json 因为crypto包必须是构建的一部分。

事实证明,在编译过程中,Angular 似乎决定安装crypto-browserify包而不是crypto.

将以下内容添加到tsconfig.json文件中会指示构建在crypto-browserify每次crypto需要时使用该库。如您所见,我对stream包裹有同样的问题。

"paths": {
  "crypto": [
    "node_modules/crypto-browserify"
  ],
  "stream": [
    "node_modules/stream-browserify"
  ]
}
Run Code Online (Sandbox Code Playgroud)


Oll*_*rca 15

在 Angular 11 和 crypto-js 4 遇到同样的问题(并在 tsconfig.json 中手动设置路径)后,我发现将 crypto-js 回滚到版本 3.1.9-1 解决了这个问题。似乎在第 4 版中所做的更改导致了该问题。

npm install crypto-js@3.1.9-1
Run Code Online (Sandbox Code Playgroud)

在回购问题中解释如下:

GitHub 问题


R. *_*rds 9

最近,我在尝试使用的一个小项目中尝试使用另一个库(tiff.js)时遇到了类似的问题。

我解决此问题的方法是在本节之后立即将以下内容添加到我的package.json文件中devDependencies。

"devDependencies": {
    ...
},
"browser": {
    "crypto": false
}
Run Code Online (Sandbox Code Playgroud)

尝试在应用程序中使用库时,这似乎没有任何不利影响。

  • 很好的解决方案,除非您确实需要使用加密库。 (6认同)
  • 在我的例子中,我使用 webpack 5。我收到一条警告,解释了为什么会发生这种情况以及该怎么做:`重大更改:webpack < 5 默认情况下用于包含 node.js 核心模块的 polyfill。这已不再是这种情况。验证您是否需要此模块并为其配置一个polyfill。如果你想包含一个polyfill,你需要: - 添加一个后备 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }' - 安装 'crypto-browserify' 如果你不这样做想要包含一个polyfill,你可以使用一个空模块,如下所示:resolve.fallback: { "crypto": false }` (6认同)
  • 那么resolve.fallback放在哪里呢?angular.json 在其中一层? (4认同)
  • 现在有一个名为“crypto-es”的 ES 模块版本。它清除这些警告。https://www.npmjs.com/package/crypto-es (3认同)
  • 发生这种情况的原因是因为当您运行ng serve时,它将捆绑您package.json中的依赖项,以便将其提供给浏览器。但是,“ package.json”通常同时包含服务器和浏览器的依赖关系,并且任何本机模块的依赖关系(例如“ crypto”)都不能捆绑在浏览器上使用。在package.json的“浏览器”部分中设置“ crypto”:false会告诉“ ng serve”不要试图捆绑该文件。 (2认同)

Luc*_*oli 8

如果您升级到 Webpack 5,则需要将其添加到您的webpack 配置文件中:

resolve: {
    fallback: { crypto: false },
},
Run Code Online (Sandbox Code Playgroud)

  • @LucaFagioli 我已经看到了这个解决方案,但我没有 webpack 配置,或者至少我没有看到它 (2认同)

Jos*_*ins 5

我喜欢R. Richards的回答,但我认为提供更多信息会很有用。

这是Angular的一个已知问题,并且Angular CLI开发团队似乎认为这是功能而不是错误。我以及本主题中的其他开发人员均不同意。该线程的提供者提供了几种解决方法,但在实施R. Richards的解决方案之前,我的项目无法成功编译。不过,我没有还原以前的更改,因此tacnoman和GrandSchtroumpf的修复程序可能对其他人有用。

有些人(例如此处的clovis1122和该问题线程中的其他人)质疑为什么Web应用程序需要访问这些库,以及为什么不能在服务器端完成必要的任务。我不能代表所有人,但我的用例是,在对用户帐户进行身份验证时,Strapi将使用必须由客户端解码的JSON Web令牌字符串进行响应。由于必要的库取决于crypto和stream,除非这些依赖项可用,否则您将无法提取JWT到期时间。

如果有人无法从R. Richards的答案推断出问题,则必须将出现在“无法解析x”错误中的所有依赖项设置为false。例如,我的package.json的关键部分是:

    "browser": {
        "crypto": false,
        "stream": false
    }
Run Code Online (Sandbox Code Playgroud)