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

时间:2019-07-13 01:48:36

标签: react-native expo web3js

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

  

“ node_modules / web3-eth-accounts / src / index.js”中的软件包已尝试   导入节点标准库模块“ 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

2 个答案:

答案 0 :(得分:0)

我为您提供2个答案:)

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

2)从您提供给项目的链接中复制垫片,并将其导入顶部的App.js文件中,以便您的web3库正常工作

答案 1 :(得分:0)

您可以使用.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

Structure_1 Structure_2 App.js