简体   繁体   English

错误:尝试导入节点标准库模块“crypto”。 它失败了

[英]Error: attempted to import the Node standard library module “crypto”. It failed

I was going to use ethereumjs-wallet . 我打算用ethereumjs-wallet But I decided to use ethereumjs-wallet-react-native because it's for the web. 但我决定使用ethereumjs-wallet-react-native因为它适用于网络。 If you look at the link , it says import the two modules. 如果你查看链接 ,它会说import两个模块。 So I took two modules and tried to import them, but there was a problem. 所以我拿了两个模块并尝试import它们,但是有一个问题。

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" 无法解析模块“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. 但是,当我们调用并执行ethereumjs-wallet-react-native.时,还有另一个问题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". “node_modules / ethereumjs-wallet-react-native / index.js”中的包试图导入节点标准库模块“crypto”。 It failed because React Native does not include the Node standard library. 它失败了,因为React Native不包含Node标准库。 Read more at https://docs.expo.io/versions/latest/introduction/faq/#can-i-use-nodejs-packages-with-expo 更多信息, 访问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. 所以我下载了crypto-browserify ,但它没有用。

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: 这是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. “ethereumjs-wallet-react-native / index.js”的代码位于我文章的链接中。

Please help us. 请帮助我们。 Thank you in advance. 先感谢您。

react-native-randombytes were Re-link. react-native-randombytes是Re-link。

and

I was used import crypto from "crypto"; import crypto from "crypto";使用了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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导入 Node 标准库模块“crypto”时出错 - Error importing the Node standard library module “crypto” 尝试导入错误,无法编译反应应用程序 - Attempted import error, failed to compile react app 构建编译失败 - 尝试导入错误 - Build failed to compile - Attempted import error 找不到模块:错误:您试图导入,相对导入有问题 - Module not found: Error: You attempted to import , problems with relative import React JS导入错误-找不到模块:您试图导入不在项目src /目录下的../actions - React JS import error - Module not found: You attempted to import ../actions which falls outside of the project src/ directory Node.js加密模块 - Node.js crypto module 加密模块-Node.js - Crypto module - Node.js 尝试导入错误:'未从 - Attempted import error:' is not exported from 编译失败。/src/Content.js 尝试导入错误:“重定向”未从“react-router-dom”导出 - Failed to compile ./src/Content.js Attempted import error: 'Redirect' is not exported from 'react-router-dom' 未找到模块:错误:您尝试导入位于项目 src/ 目录之外的 babel-preset - Module not found: Error: You attempted to import babel-preset which falls outside of the project src/ directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM