【问题标题】:Error: attempted to import the Node standard library module "crypto". It failed错误:试图导入 Node 标准库模块“crypto”。它失败了
【发布时间】:2019-09-13 05:46:08
【问题描述】:

我打算使用ethereumjs-wallet。但我决定使用ethereumjs-wallet-react-native,因为它是用于网络的。如果您查看the link,它会显示import 这两个模块。于是我拿了两个模块尝试import他们,但是出现了问题。

导致错误的代码:

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

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

错误信息:

无法解析模块“react-native-secure-randombytes”

我想解决这个问题,但是解决不了,所以我决定绕道而行。

错误解决代码:

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

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

这可能会在以后引起问题,但现在已经解决了。然而,当我们调用并执行ethereumjs-wallet-react-native.时,又出现了一个问题

错误消息:

“node_modules/ethereumjs-wallet-react-native/index.js”的包 试图导入 Node 标准库模块“crypto”。它 失败是因为 React Native 不包含 Node 标准 图书馆。阅读更多 https://docs.expo.io/versions/latest/introduction/faq/#can-i-use-nodejs-packages-with-expo

于是我下载了crypto-browserify,但还是不行。

尝试解析的代码

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();
  }

这是 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"
  },

“ethereumjs-wallet-react-native/index.js”的代码在我的文章中位于the link

请帮助我们。提前谢谢你。

【问题讨论】:

  • but it didn't work - 以什么方式?
  • @Jaromanda X 错误信息是相同的错误
  • 所以,你仍然得到attempted to import the Node standard library module "crypto" - 奇怪,因为你没有尝试那个 - 哦,等等,你当然是,因为它是 ethereumjs-wallet-react-native 导入 crypto - 它会总是尝试做,除非你改变它,所以它不会
  • @Jaromanda X 我刚刚遵循了 Github 指南。你说我没试过?
  • 我没有说你没有尝试任何东西......我说你没有改变任何关于 ethereumjs-wallet-react-native 正在导入的内容

标签: javascript react-native node-modules expo ethereum


【解决方案1】:

react-native-randombytes 被重新链接。

我被import crypto from "crypto";使用

重新创建成功的代码

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;

【讨论】:

    猜你喜欢
    • 2020-10-26
    • 2019-11-22
    • 2021-05-05
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    • 2018-01-15
    • 2021-05-19
    • 2020-11-13
    相关资源
    最近更新 更多