Home > Software engineering >  How do I resolve 'Null is not an object' error when evaluating RNRandomBytes.seed
How do I resolve 'Null is not an object' error when evaluating RNRandomBytes.seed

Time:05-12

I'm trying to build a mobile application with expo and react-native, I have a screen where I need to generate a passphrase for the user and I'm using react-native-bip39 in order to do that.

After installing the following packages: react-native-bip39, react-native-crypto and react-native-randombytes, I had an error that says:

Unable to resolve module stream

After doing some digging, I understood that I could use rn-nodeify to install the missing packages, however when I did install it, the following error has been generated and I can't seem to find a solution for it:

TypeError: null is not an object (evaluating 'RNRandomBytes.seed') at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException at node_modules/react-native/Libraries/Core/ExceptionsManager.js:172:19 in handleException at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError at node_modules/@react-native/polyfills/error-guard.js:49:36 in ErrorUtils.reportFatalError at node_modules/metro-runtime/src/polyfills/require.js:204:6 in guardedLoadModule at http://192.168.18.160:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&strict=false&minify=false:222696:3 in global code

Followed by another one on the terminal:

Invariant Violation: "main" has not been registered. This can happen if: Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project. A module failed to load due to an error and AppRegistry.registerComponent wasn't called. at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException at node_modules/react-native/Libraries/Core/ExceptionsManager.js:172:19 in handleException at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError at node_modules/@react-native/polyfills/error-guard.js:49:36 in ErrorUtils.reportFatalError

There is a shim.js file generated by rn-nodeify in the root folder, and package.json has more dependencies installed by the same tool.

How can I resolve this issue?

CodePudding user response:

I had the same problem and after some research, this is what did work for me:

1- Adding buffer and events Error: Unable to resolve module `buffer` React Native

2- Seed problem: switch from react-native-randombytes to react-native-get-random-values TypeError: null is not an object (evaluating 'RNRandomBytes.seed') React Native

3- Fix stream problem in cipher-base with readable-stream by adding 'patch-package' https://github.com/crypto-browserify/cipher-base/issues/10

And finally this is how I implemented the solution in a custom hook to get my random words :

import { useState, useEffect } from "react";
import "react-native-get-random-values";
import { entropyToMnemonic } from "bip39";

export default function UseBip39() {
  const [state, setState] = useState([]);

  useEffect(() => {
    async function generateWords() {
      const entropy = await crypto.getRandomValues(new Uint8Array(16));
      setState(entropyToMnemonic(entropy).split(" "));
    }
    generateWords();
  }, []);

  return state;
}
  • Related