[英]Unable to resolve module crypto in reactnative
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.