question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React Native: crypto module problem

See original GitHub issue

Crypto dependency problem

on React Native

Having upgraded to React Native 0.65.1, I now get the following error when building the app:

error: Error: Unable to resolve module crypto from /path/project/node_modules/@walletconnect/randombytes/dist/cjs/node/index.js: crypto could not be found within the project or in these directories:
  node_modules

If you are sure the module exists, try these steps:
 1. Clear watchman watches: watchman watch-del-all
 2. Delete node_modules and run yarn install
 3. Reset Metro's cache: yarn start --reset-cache
 4. Remove the cache: rm -rf /tmp/metro-*
   5 | Object.defineProperty(exports, "__esModule", { value: true });
   6 | exports.randomBytes = void 0;
>  7 | const crypto_1 = __importDefault(require("crypto"));
     |                                           ^
   8 | const encoding_1 = require("@walletconnect/encoding");
   9 | function randomBytes(length) {
  10 |     const buf = crypto_1.default.randomBytes(length);

Some discussion about this on stackoverflow: https://stackoverflow.com/a/64363788

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:18

github_iconTop GitHub Comments

6reactions
ziadTahacommented, Jan 13, 2022

following the workflow from react-native-crypto https://github.com/tradle/react-native-crypto

npm i --save react-native-crypto

install peer deps

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

on RN >= 0.60, instead do: cd iOS && pod install

install latest rn-nodeify

npm i --save-dev 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

it should work fine

5reactions
benorgeracommented, May 16, 2022

This is the issue of react native running in browser environment, not having the Node.js builtin deps. See here or here. To fix:

npm i --save-dev rn-nodeify

Add postinstall script to nodemodules:

"scripts": {
  …
  "postinstall": "node_modules/.bin/rn-nodeify --install crypto --hack"
}

Then npm install.

Note if you have this issue with multiple Node.js builtin packages (ie. streams) you can hack them inline as

"postinstall": "node_modules/.bin/rn-nodeify --install crypto --hack && node_modules/.bin/rn-nodeify --install stream --hack"

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to resolve module crypto in reactnative - Stack Overflow
Crypto is a node js module, when React Native is run - it uses Javascript Core. Crypto isn't include within this. When I...
Read more >
Unable to resolve module crypto - DEV Community ‍ ‍
The gist of it is that React Native does not use the Node runtime environment and thus does not have access to specific...
Read more >
Can't resolve 'crypto' in node_modules/bson/dist react
I'm using realm-web on my react app to build a simple task reminder app. ... The app is running fine but I'm having...
Read more >
unable to resolve module assets react native - You.com
1. I had the same problem, I fixed that by below steps: 1-> delete node_modules by: rm -rf node_modules. 2-> delte Pods ...
Read more >
Crypto - Expo Documentation
If you're installing this in a bare React Native app, you should also follow these ... return ( <View style={styles.container}> <Text>Crypto Module ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found