Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: attempted to import the Node standard library module "crypto". It failed

I was going to use ethereumjs-wallet. But I decided to use ethereumjs-wallet-react-native because it's for the web. If you look at the link, it says import the two modules. So I took two modules and tried to import them, but there was a problem.

an error-causing code:

import { asyncRandomBytes } from 'react-native-secure-randombytes'
import safeCrypto from 'react-native-safe-crypto'

window.randomBytes = asyncRandomBytes
window.scryptsy = safeCrypto.scrypt

Errormessage:

Unable to resolve module "react-native-secure-randombytes"

I wanted to solve this problem, but I couldn't solve it, so I decided to take a detour.

Error Resolved Code:

import safeCrypto from "react-native-fast-crypto";
const { RNRandomBytes } = NativeModules;

window.randomBytes = RNRandomBytes.randomBytes;
window.scryptsy = safeCrypto.scrypt;

This may cause problems later, but it's settled for now. However, there was another problem when we called up and executed ethereumjs-wallet-react-native.

errormessage:

The package at "node_modules/ethereumjs-wallet-react-native/index.js" attempted to import the Node standard library module "crypto". It failed because React Native does not include the Node standard library. Read more at https://docs.expo.io/versions/latest/introduction/faq/#can-i-use-nodejs-packages-with-expo

So I downloaded crypto-browserify, but it didn't work.

Code that attempted to resolve

import safeCrypto from "react-native-fast-crypto";
const { RNRandomBytes } = NativeModules;
import Wallet from "ethereumjs-wallet-react-native";
var crypto = require("crypto-browserify");

window.randomBytes = RNRandomBytes.randomBytes;
window.scryptsy = safeCrypto.scrypt;
...
 async componentDidMount() {
    const wallet = await Wallet.generate();
  }

this is package.json:

  "dependencies": {
    "crypto-browserify": "^3.12.0",
    "ethereumjs-wallet-react-native": "^0.6.7",
    "expo": "^32.0.0",
    "expokit": "32.1.1",
    "react": "16.5.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
    "react-native-fast-crypto": "^1.8.1",
    "react-native-randombytes": "^3.5.2"
  },
  "devDependencies": {
    "babel-preset-expo": "^5.0.0"
  },

The code for "ethereumjs-wallet-react-native/index.js" is located on the link in my article.

Please help us. Thank you in advance.

like image 993
hong developer Avatar asked Oct 29 '25 06:10

hong developer


1 Answers

react-native-randombytes were Re-link.

and

I was used import crypto from "crypto";

Recreate Successful Code

import crypto from "crypto";
import safeCrypto from "react-native-fast-crypto";
import { asyncRandomBytes } from "react-native-secure-randombytes";
...
window.randomBytes = asyncRandomBytes;
window.scryptsy = safeCrypto.scrypt;
like image 80
hong developer Avatar answered Oct 31 '25 01:10

hong developer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!