簡體   English   中英

無法在反應式中解析模塊加密

[英]Unable to resolve module crypto in reactnative

我在這里發布了這個已經創建了 react-native 應用程序使用

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

但我收到錯誤消息,無法從 web3-eth-accounts 解析模塊加密。 有沒有什么辦法解決這一問題

無法解析加密在此處輸入圖像描述

Crypto 是一個節點 js 模塊,當 React Native 運行時 - 它使用 Javascript Core。 加密不包括在其中。 當我安裝加密時,我使用了以下包:

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

指示:

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

在 index.js 文件中導入shim.js

完成后,應該可以使用加密,如果它仍然不起作用,我必須在我的 App.js 文件中創建一個 const,如下所示:

export const cryp = require('crypto');

並將其導入您需要的組件中。

更新

我為此做了一個全新的構建,我遵循以下內容:

react-native init TestApp

按照上面的說明進行加密。

鏈接:

react-native link

react-native run-ios

react-native-crypto不再適用於最近的 react-native 版本0.63.3和 react 版本16.13.1

我使用crypto-js包。 我的 react-native 應用程序中的版本是3.1.9-1 它運作良好。 您可以在package.json文件中添加以下行。

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

crypto 是一個與瀏覽器一起使用的節點庫,但是我們可以將它與 react native 一起使用,下面提到的一些 hack 遵循這些步驟並繁榮! 你准備好搖滾了。

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

我有同樣的問題,似乎 react native 不支持加密模塊,因為當我安裝 crytpo 時,它在 node_modules 中沒有 index.js 文件。 所以當我嘗試使用使用加密來加密數據的 jsonwebtoken 時,我的問題就出現了。 所以我卸載了 jsonwebtoken 並切換到 react-native-pure-jwt

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM