Srinil Khandwala
Srinil Khandwala

Reputation: 232

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 : enter image description here

I tried installing the crypto-browserify package but that too didn't resolve the issue.

Upvotes: 14

Views: 54280

Answers (7)

Raghav Sharma
Raghav Sharma

Reputation: 1100

To resolve this issue in the latest React 18.2.0 version.

You first need to install "crypto-browserify".

Use this command to install =>

npm install crypto-browserify

After the installation go to " webpack.config.js".

You will find this file in the node_modules > react-scripts > config > webpack.config.js.

Inside webpack.config.js

Paste =>

const path = require('path');

Then search for module.exports and inside module.exports search for resolve: {} and paste the code like this:-

resolve: {
      fallback: {
        crypto: require.resolve('crypto-browserify'),
      }, 

Save the file and run your project.

Upvotes: 1

Krishnapal Singh
Krishnapal Singh

Reputation: 1

You can use old vezgo sdk version.

You can install it by doing

npm i [email protected]

Upvotes: 0

Rokit
Rokit

Reputation: 1141

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.

npm i jwt-decode

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

Upvotes: 8

Mohit Prajapati
Mohit Prajapati

Reputation: 440

I was facing the same issue. I was trying to generate the keys on in React and was getting the same issue. Now the issue is resolved. Just follow the steps given in link https://github.com/ChainSafe/web3.js#troubleshooting-and-known-issues Also add these two in package.json

"dependencies": {
   "crypto": "npm:crypto-browserify",
   "stream": "npm:stream-browserify",
}

Upvotes: 12

Antonio Roldan
Antonio Roldan

Reputation: 48

For me installing crypto-browserify worked flawlessly. 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

This npm will install almost all dependencies of node.js crypto module, check what are available and what are not in case you need more information.

Upvotes: 1

Muhammad Hassan Khan
Muhammad Hassan Khan

Reputation: 149

Try installing crypto-browserify package and see if that works. If you still see that error try deleting your node_modules folder and install all packages again.

Upvotes: 0

Sayak China
Sayak China

Reputation: 181

Try adding the following just after the devDependencies in your package.json

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

Upvotes: 14

Related Questions