【发布时间】:2020-07-02 10:07:27
【问题描述】:
【问题讨论】:
-
你在使用任何加密库吗?
-
没有。我想用mqtt
-
好的,有一种方法可以解决您的问题
-
这个问题解决了吗?
-
npm i -S 缓冲区;从“缓冲区”导入{缓冲区};为我工作。
标签: react-native
【问题讨论】:
标签: react-native
所以缓冲区是 Node JS 的核心模块,这意味着它可能是与 Node JS 二进制文件捆绑在一起的 C++ 代码,而不是 Javascript。 React Native 打包器无法将其与您应用的 Javascript 包一起打包,因此您会收到运行时错误:无法解析模块。
要解决此问题,您需要这样做以使其在 react-native 中运行
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
Now you need to run this command in your project directory
./node_modules/.bin/rn-nodeify --hack --install
最后一步
在你的 app.js 中导入这个
import './shim.js'
在 packaje.json
把这个postinstall:./node_modules/.bin/rn-nodeify --hack --install
【讨论】:
App.js 解析模块./shim.js
./node_modules/.bin/rn-nodeify --hack --install
react_native_mqtt。但请务必使用代理的 Web 套接字端口。否则它甚至不会连接。
您可能需要安装 nodejs 缓冲区。 (npm i -S buffer)
还要检查this answer
【讨论】:
global.Buffer = global.Buffer || require ('buffer').Buffer 时,出现以下错误:TypeError: undefined is not an object (evaluating 'process.version.slice')