简体   繁体   English

无法在反应式中解析模块加密

[英]Unable to resolve module crypto in reactnative

I have posted this here have created react-native app using我在这里发布了这个已经创建了 react-native 应用程序使用

react-native init myapp
added web3 in package.json
npm install
react-native run-ios

but i am getting the error unable to resolve module crypto from web3-eth-accounts.但我收到错误消息,无法从 web3-eth-accounts 解析模块加密。 Is there any way to fix this有没有什么办法解决这一问题

unable to resolve crypto无法解析加密在此处输入图像描述

Crypto is a node js module, when React Native is run - it uses Javascript Core. Crypto 是一个节点 js 模块,当 React Native 运行时 - 它使用 Javascript Core。 Crypto isn't include within this.加密不包括在其中。 When I installed crypto I used the following package:当我安装加密时,我使用了以下包:

https://www.npmjs.com/package/react-native-crypto https://www.npmjs.com/package/react-native-crypto

Instructions:指示:

npm i --save react-native-crypto
# install peer deps 
npm i --save react-native-randombytes
react-native link react-native-randombytes
# install latest rn-nodeify 
npm i --save-dev tradle/rn-nodeify
# install node core shims and recursively hack package.json files 
# in ./node_modules to add/update the "browser"/"react-native" field with relevant mappings 
./node_modules/.bin/rn-nodeify --hack --install
rn-nodeify will create a shim.js in the project root directory
// index.ios.js or index.android.js
// make sure you use `import` and not require!  
import './shim.js'
// ...the rest of your code

Import shim.js in your index.js file.在 index.js 文件中导入shim.js

When you have done that crypto should be made available, if it still doesn't work I had to create a const in my App.js file like so:完成后,应该可以使用加密,如果它仍然不起作用,我必须在我的 App.js 文件中创建一个 const,如下所示:

export const cryp = require('crypto');

And import it into the components you need.并将其导入您需要的组件中。

UPDATE更新

I've done a fresh build for this, I followed the below:我为此做了一个全新的构建,我遵循以下内容:

react-native init TestApp

Follow the instructions above for Crypto.按照上面的说明进行加密。

Linked:链接:

react-native link

react-native run-ios

react-native-crypto don't work on recent react-native version 0.63.3 and react version 16.13.1 , any more. react-native-crypto不再适用于最近的 react-native 版本0.63.3和 react 版本16.13.1

I used crypto-js package.我使用crypto-js包。 The version is 3.1.9-1 in my react-native app.我的 react-native 应用程序中的版本是3.1.9-1 It's working well.它运作良好。 You can add below line in package.json file.您可以在package.json文件中添加以下行。

"crypto-js": "3.1.9-1",

crypto is a node's library that works with the browser however we can use it with react native with some hacks mentioned below follow these steps and boom! crypto 是一个与浏览器一起使用的节点库,但是我们可以将它与 react native 一起使用,下面提到的一些 hack 遵循这些步骤并繁荣! you are ready to rock.你准备好摇滚了。

npm i --save react-native-crypto

==>  install peer deps 

npm i --save react-native-randombytes
react-native link react-native-randombytes

==>install latest rn-nodeify 
npm i --save-dev tradle/rn-nodeify

==>  install node core shims and recursively hack package.json files 
==> in ./node_modules to add/update the "browser"/"react-native" fieldwith relevant mappings 
./node_modules/.bin/rn-nodeify --hack --install

i had the same problem, it seems the crypto module is not supported by react native because when i install crytpo , it does not have an index.js file in the node_modules.我有同样的问题,似乎 react native 不支持加密模块,因为当我安装 crytpo 时,它在 node_modules 中没有 index.js 文件。 so my problem came about when i was trying to use jsonwebtoken which uses crypto to encrypt data.所以当我尝试使用使用加密来加密数据的 jsonwebtoken 时,我的问题就出现了。 so i uninstalled jsonwebtoken and switched to react-native-pure-jwt所以我卸载了 jsonwebtoken 并切换到 react-native-pure-jwt

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM