Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 cryptoenter image description here

like image 208
Trinu Avatar asked Oct 08 '18 07:10

Trinu


2 Answers

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

like image 50
JRK Avatar answered Sep 18 '22 12:09

JRK


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",
like image 45
NinjaDev Avatar answered Sep 16 '22 12:09

NinjaDev