简体   繁体   English

找不到模块:错误:无法解析'crypto' React

[英]Module not found: Error: Can't resolve 'crypto' React

I installed the jsonwebtoken package and ever since I'm getting the following error on running the React app:我安装了jsonwebtoken package 自从我在运行 React 应用程序时遇到以下错误: 在此处输入图像描述

I tried installing the crypto-browserify package but that too didn't resolve the issue.我尝试安装crypto- browserify package 但这也没有解决问题。

Try adding the following just after the devDependencies in your package.json尝试在 package.json 中的 devDependencies 之后添加以下内容

"devDependencies": {
    ...
},
"browser": {
    "crypto": false
}

Try installing crypto-browserify package and see if that works.尝试安装crypto-browserify browserify package 看看是否可行。 If you still see that error try deleting your node_modules folder and install all packages again.如果您仍然看到该错误,请尝试删除您的 node_modules 文件夹并重新安装所有软件包。

For me installing crypto-browserify worked flawlessly.对我来说,安装 crypto-browserify 完美无缺。 You can do this by running the command yarn add crypto-browserify or if you prefer you can install it by doing npm i crypto-browserify.您可以通过运行命令 yarn add crypto-browserify 来执行此操作,或者如果您愿意,可以通过执行 npm i crypto-browserify 来安装它。 This npm will install almost all dependencies of node.js crypto module, check what are avaliables and what are not in case you need more information.这个 npm 将安装 node.js 加密模块的几乎所有依赖项,检查哪些是可用的,哪些是不可用的,以防您需要更多信息。

I was facing the same issue.我面临着同样的问题。 I was trying to generate the keys on in React and was getting the same issue.我试图在 React 中生成密钥并且遇到了同样的问题。 Now the issue is resolved.现在问题解决了。 I used this package to generate the keys and then updated my package.json with these packages.我使用这个package来生成密钥,然后用这些包更新了我的 package.json。

   "dependencies": {
    "assert": "^2.0.0",
    "buffer": "^6.0.3",
    "crypto": "npm:crypto-browserify",
    "crypto-browserify": "^3.12.0",
    "https-browserify": "^1.0.0",
    "os-browserify": "^0.3.0",
    "stream": "npm:stream-browserify",
    "stream-browserify": "^3.0.0",
    "stream-http": "^3.2.0",
    "web3": "^1.6.1"
  },

If you just want to decode JWTs in React, you can replace jsonwebtoken with jwt-decode , and then you don't have to worry about installing anything extra or editing package.json to resolve the crypto error.如果您只想在 React 中解码 JWT,您可以将jsonwebtoken替换为jwt-decode ,然后您不必担心安装任何额外的东西或编辑package.json来解决crypto错误。

npm i jwt-decode

import jwt_decode from 'jwt-decode'
let decoded = jwt_decode(token)

You can use old vezgo sdk version.您可以使用旧的 vezgo sdk 版本。

You can install it by doing您可以通过执行安装它

npm i vezgo-sdk-js@0.0.6 npm 我 vezgo-sdk-js@0.0.6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM