简体   繁体   中英

Unable to resolve module crypto in reactnative

I have posted this here have created react-native app using

react-native init myapp
added web3 in package.json
npm install
react-native run-ios

but i am getting the error unable to resolve module crypto from web3-eth-accounts. Is there any way to fix this

unable to resolve crypto在此处输入图像描述

Crypto is a node js module, when React Native is run - it uses Javascript Core. Crypto isn't include within this. When I installed crypto I used the following package:

https://www.npmjs.com/package/react-native-crypto

Instructions:

npm i --save react-native-crypto
# install peer deps 
npm i --save react-native-randombytes
react-native link react-native-randombytes
# install latest rn-nodeify 
npm i --save-dev tradle/rn-nodeify
# install node core shims and recursively hack package.json files 
# in ./node_modules to add/update the "browser"/"react-native" field with relevant mappings 
./node_modules/.bin/rn-nodeify --hack --install
rn-nodeify will create a shim.js in the project root directory
// index.ios.js or index.android.js
// make sure you use `import` and not require!  
import './shim.js'
// ...the rest of your code

Import shim.js in your index.js file.

When you have done that crypto should be made available, if it still doesn't work I had to create a const in my App.js file like so:

export const cryp = require('crypto');

And import it into the components you need.

UPDATE

I've done a fresh build for this, I followed the below:

react-native init TestApp

Follow the instructions above for Crypto.

Linked:

react-native link

react-native run-ios

react-native-crypto don't work on recent react-native version 0.63.3 and react version 16.13.1 , any more.

I used crypto-js package. The version is 3.1.9-1 in my react-native app. It's working well. You can add below line in package.json file.

"crypto-js": "3.1.9-1",

crypto is a node's library that works with the browser however we can use it with react native with some hacks mentioned below follow these steps and boom! you are ready to rock.

npm i --save react-native-crypto

==>  install peer deps 

npm i --save react-native-randombytes
react-native link react-native-randombytes

==>install latest rn-nodeify 
npm i --save-dev tradle/rn-nodeify

==>  install node core shims and recursively hack package.json files 
==> in ./node_modules to add/update the "browser"/"react-native" fieldwith relevant mappings 
./node_modules/.bin/rn-nodeify --hack --install

i had the same problem, it seems the crypto module is not supported by react native because when i install crytpo , it does not have an index.js file in the node_modules. so my problem came about when i was trying to use jsonwebtoken which uses crypto to encrypt data. so i uninstalled jsonwebtoken and switched to react-native-pure-jwt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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