Unable to resolve module crypto in reactnative
Asked Answered
C

4

16

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

Centro answered 8/10, 2018 at 7:10 Comment(0)
S
12

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

Sandstrom answered 8/10, 2018 at 8:15 Comment(6)
after executing the above steps and after running react-native run-ios i am getting CFBundleIdentifier", Does Not ExistCentro
What version of RN are you using, have you opened the project in XCode?Sandstrom
0.57 and when i try to buid in xcode build is failingCentro
I'll do a fresh build and check for you.Sandstrom
where to import this file import './shim.js' in app.js? Also can you tell me where to add this line export const cryp = require('crypto'); in app.js ?. I don't have tsx file.Centro
Let us continue this discussion in chat.Sandstrom
H
4

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",
Hernia answered 15/10, 2020 at 1:53 Comment(6)
Did you have to alias crypto-js to crypto somehow? I have crypto-js installed but metro still tells me it's unable to resolve crypto.Prophecy
@ClaudioBrasser, I didn't renamed it. I used crypto-js.Hernia
are you importing crypto yourself or is it used by a library? In my case crypto is required by a library and I want to replace it by crypto-js since crypto is not available for rn.Prophecy
hmm... I used the crypto-js to encrypt / decrypt a text. If the crypto package is required by other library, I think you maybe change another library, or should customize the library so that it import crypto-js.Hernia
I shared the example code on github.com/dev0088/Whitings-RNHernia
I met require cycle warn when using crypto-js 4.1.1: WARN Require cycle: node_modules/metro/src/node-haste/DependencyGraph/assets/empty-module.js -> src/pages/index/index.tsx -> src/utils/hzxw-api-client.ts -> node_modules/crypto-js/index.js -> node_modules/crypto-js/core.js -> node_modules/metro/src/node-haste/DependencyGraph/assets/empty-module.jsBanky
E
2

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
Enneastyle answered 21/9, 2021 at 7:35 Comment(2)
Hi, can you explain how to do the last line in your answer? How to add the shim? I have this in my bin/rn-nodify: if (toShim.indexOf('crypto') !== -1) { toShim.push('react-native-randombytes') }Comenius
Hello, @Comenius You don't have to add any files manually shim file will be automatically added in the root folder of your project by the last command ./node_modules/.bin/rn-nodeify --hack --installEnneastyle
M
2

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

Mertz answered 21/6, 2022 at 21:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.