3

Steps to reproduce:

create-react-native-app proj && cd proj && npm i

Installing package that is using crypto. Performing steps listed at https://www.npmjs.com/package/react-native-crypto :

npm i -S react-native-crypto && npm i -S react-native-randombytes
react-native link react-native-randombytes

Got a warning for react-native link failure, however after the following hack crypto dependancy is no longer a problem:

npm i --save-dev tradle/rn-nodeify
./node_modules/.bin/rn-nodeify --hack --install

Adding import './shim.js' into the App.js

yarn run ios

Got a problem like here: https://github.com/mvayngrib/react-native-randombytes/issues/13

undefined is not an object (evaluating 'RNRandomBytes.seed')

I can't update npm and node due to some of the used package restrictions

MacOS 10.12, Node 8.0.0, npm 5.0

EDIT:

RNRandomBytes are initialized as let RNRandomBytes = require('react-native').NativeModules.RNRandomBytes

init is the first place where RNRandomBytes is accessed in react-native-randombytes imported from react-native-crypto imported from twitter nacl

EDIT2: After creating project via react-native init linking phase completed successfully:

rnpm-install info Linking react-native-randombytes android dependency 
rnpm-install info Android module react-native-randombytes has been successfully linked 
rnpm-install info Linking react-native-randombytes ios dependency 
rnpm-install info iOS module react-native-randombytes has been successfully linked 

import './shim.js' for this sample project is in the index.js instead of App.js

But the error was the same

user1232690
  • 481
  • 5
  • 16

1 Answers1

4

The RNRandomBytes variable is not defined, because it should be exported by a native module, and you haven't linked the module.

The react-native link step fails, because you've initialised your project using create-react-native-app, which is based on Expo, and does not allow linking custom native dependencies.

If you want to use this library (or other React Native libraries with native dependencies) you'll need to either eject from the Expo app, or initialise your project with react-native init.

You can read more about the difference between create-react-native-app and react-native init here: whats the real diff between "create-react-native-app myproject" and "react-native init myproject".

jevakallio
  • 35,324
  • 3
  • 105
  • 112
  • Thanks you for a very detailed explanation! I'll check if it will work for me right now – user1232690 Feb 07 '18 at 18:36
  • 1
    I've created a sample project with `react-native init` (linking doesn't fail anymore) and added shim.js to the index.js, but the error is still there. Do you have any suggestions? – user1232690 Feb 07 '18 at 19:04
  • Update. I tried on another mac and it worked. Looks like node installation was broken on this one. Thank you again, I really appreciate it! – user1232690 Feb 08 '18 at 13:33
  • I am also facing the same error but i want to solve it on my machine I don't have another machine – Chandni Jan 13 '21 at 01:43
  • 1
    @jevakallio is there a way to use node std libs like 'crypto' with expo without having to eject? – Hyetigran Aug 10 '21 at 09:01