0

I have React Native project, where I installed a npm package, which used itself crypto module of node (require('crypto')) and it causing issues with React Native. is there any modern / clean way to somehow resolve this part, and use React Native compatible library, where node crypto is "required"?

1 Answer 1

0

You can use rn-nodeify package which allow you to use node core modules in your React Native app.

npm install rn-nodeify

install specific shims and hack

rn-nodeify --install "fs,dgram,process,path,console" --hack

It is recommended to add this command to the "postinstall" script in your project's package.json

"scripts": {
  "start": "node node_modules/react-native/local-cli/cli.js start",
  "postinstall": "rn-nodeify --install fs,dgram,process,path,console --hack"
}

rn-nodeify will create a shim.js file in your project root directory. The first line in index.ios.js / index.android.js should be to import it (NOT require it!)

import './shim'

Example

// index.ios.js or index.android.js
// make sure you use `import` and not `require`!
import './shim.js'
// ...the rest of your code
import crypto from 'crypto'
// use crypto
console.log(crypto.randomBytes(32).toString('hex'))

for more details Visit nodeify package

Hopefully it will resolve your problem.

Sign up to request clarification or add additional context in comments.

2 Comments

The problem is that, one of the npm packages uses require('crypto'), and that's causing problem
could you share which npm package required crypto? one suggestion: you can use same package and can change accordingly in node-module of that package which required crypto and make its patch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.