【问题标题】:“node_modules/web3-eth-accounts/src/index.js”中的包试图导入Node标准库模块“crypto”
【发布时间】:2019-11-22 17:29:39
【问题描述】:

我正在 React Native 中构建一个以太坊 ERC20 代币支持钱包,过去几天我一直在努力解决这个特定问题,希望有人能提供帮助。

“node_modules/web3-eth-accounts/src/index.js”中的包尝试 导入 Node 标准库模块“crypto”

这是我的 package.json 文件。

"dependencies": {
    "expo": "^33.0.0",
    "react": "16.8.3",
    "react-dom": "^16.8.6",
    "react-native": "https://github.com/expo/react-native/archive/sdk-33.0.0.tar.gz",
    "react-native-web": "^0.11.4",
    "socket.io-client": "^2.2.0",
    "web3": "1.0.0-beta.34",
    "cryptico": "^1.0.2",
    "native-base": "2.8.0",
    "node-libs-browser": "2.1.0",
    "react-native-crypto": "^2.0.0",
    "react-native-randombytes": "^2.0.0"
  },
  "devDependencies": {
    "babel-preset-expo": "^5.1.1",
    "babel-cli": "6.26.0",
    "babel-preset-es2015": "6.24.1"
  },

请指教! 我也关注了这个链接,没有运气! https://gist.github.com/dougbacelar/29e60920d8fa1982535247563eb63766

【问题讨论】:

    标签: react-native expo web3js


    【解决方案1】:

    你可以使用.babelrc文件定义可选配置,首先安装依赖:

    你可以试试安装npm install --save crypto-browserify

    然后将插件配置添加到您的.babelrc 文件中:

    {
      "presets": ["babel-preset-expo"],
      "env": {
        "development": {
          "plugins": ["transform-react-jsx-source"]
        }
      },
      "plugins": [
        ["module-resolver", {
          "root": ["./app"],
          "alias": {
            "crypto": "crypto-browserify"
          }
        }]
      ]
    }
    

    请导入钱包模块。

    import crypto from 'crypto'
    

    你可以运行npm i --save-dev rn-nodeify@latestrn-nodeify --install "stream"

    如果不是./node_modules/.bin/rn-nodeify --install "stream"

    rn-nodeify会在项目根目录下创建shim.js

    例子

    // index.ios.js or index.android.js or App.js
    // make sure you use `import` and not `require`!
    import './shim.js'
    

    Error

    【讨论】:

    • 安装crypto-browserify的时候提示这个错误。
    • 我在屏幕上看不到任何错误代码,只有错误消息,我已将其截取并附在此处。请指教
    • 我认为这里需要“vm”。我收到一个错误,提示“vm”丢失。
    • 我想这对你来说可能更容易给我建议。我附上了我当前文件夹结构的屏幕截图,你看到这里有什么遗漏吗?我仍然收到有关“流”丢失的错误投诉。
    • ./node_modules/.bin/rn-nodeify --install "vm"
    【解决方案2】:

    我有 2 个答案给你 :)

    1) 使用 ethers.js,因为它很丰富并且默认支持 React Native

    2) 从您提供给项目的链接中复制 shims 并将其导入到顶部的 App.js 文件中,以便您的 web3 库应该可以工作

    【讨论】:

    • 嗨,ethers.js 在 RN 0.63 应用程序上运行有多好?似乎需要破解才能使 web3.js 与 React Native 应用程序一起使用。
    猜你喜欢
    • 2021-05-05
    • 2020-10-26
    • 2019-09-13
    • 2021-12-03
    • 1970-01-01
    • 2021-02-17
    • 2019-10-30
    • 2019-09-04
    • 2019-08-27
    相关资源
    最近更新 更多