【问题标题】:Unable to resolve module crypto in reactnative无法在本机反应中解析模块加密
【发布时间】:2019-03-12 19:40:59
【问题描述】:

我在这里发布了这个已经创建了 react-native 应用程序使用

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

但我收到无法从 web3-eth-accounts 解析模块加密的错误。有没有办法解决这个问题

无法解析加密

【问题讨论】:

    标签: react-native npm webpack ethereum web3js


    【解决方案1】:

    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 文件中创建一个常量,如下所示:

    export const cryp = require('crypto');
    

    并将其导入到您需要的组件中。

    更新

    我已经为此做了一个全新的构建,我遵循以下内容:

    react-native init TestApp

    按照上述有关加密的说明进行操作。

    链接:

    react-native link

    react-native run-ios

    【讨论】:

    • 执行上述步骤并运行 react-native run-ios 后,我得到 CFBundleIdentifier",不存在
    • 你用的是什么版本的RN,你在XCode中打开过项目吗?
    • 0.57,当我尝试在 xcode 中构建时失败
    • 我会重新构建并为您检查。
    • 在哪里导入这个文件 import './shim.js' in app.js?你也能告诉我在哪里添加这一行 export const cryp = require('crypto');在 app.js 中?我没有 tsx 文件。
    【解决方案2】:

    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-js 别名为crypto?我安装了crypto-js,但 Metro 仍然告诉我它无法解析加密。
    • @ClaudioBrasser,我没有重命名它。我用crypto-js
    • 您是自己导入crypto 还是被图书馆使用?在我的情况下,库需要 crypto,我想用 crypto-js 替换它,因为 crypto 不适用于 rn。
    • hmm... 我使用crypto-js 来加密/解密文本。如果其他库需要 crypto 包,我想你可能会更改另一个库,或者应该自定义库以便导入 crypto-js
    • 我在github.com/dev0088/Whitings-RN分享了示例代码
    【解决方案3】:

    crypto 是一个与浏览器一起工作的节点库,但是我们可以将它与 react native 一起使用,下面提到的一些黑客遵循这些步骤,然后繁荣!你准备好了。

    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
    

    【讨论】:

    • 嗨,你能解释一下如何在你的答案中做最后一行吗?如何添加垫片?我的 bin/rn-nodify 中有这个: if (toShim.indexOf('crypto') !== -1) { toShim.push('react-native-randombytes') }
    • 你好,@Bomber 你不必手动添加任何文件 shim 文件将通过最后一条命令自动添加到项目的根文件夹中./node_modules/.bin/rn-nodeify --hack --install
    猜你喜欢
    • 2016-02-03
    • 1970-01-01
    • 2021-09-13
    • 2021-09-03
    • 1970-01-01
    • 2023-03-13
    • 2022-01-04
    • 1970-01-01
    • 2018-05-28
    相关资源
    最近更新 更多