【问题标题】:React-Native: Can't find variable: BufferReact-Native:找不到变量:缓冲区
【发布时间】:2020-07-02 10:07:27
【问题描述】:

我对变量 Buffer 有疑问。您可以在下图中看到它。 有谁能够帮我???

谢谢。

【问题讨论】:

  • 你在使用任何加密库吗?
  • 没有。我想用mqtt
  • 好的,有一种方法可以解决您的问题
  • 这个问题解决了吗?
  • npm i -S 缓冲区;从“缓冲区”导入{缓冲区};为我工作。

标签: react-native


【解决方案1】:

所以缓冲区是 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
  • max 提供运行此命令时创建的 shim.js 的路径./node_modules/.bin/rn-nodeify --hack --install
  • 这个答案解决了问题吗?另外,您使用的是哪个 react-native 库?
  • @WISERDIVISOR 阅读灰色背景中的文本所有需要安装的库都被长期提及
  • @RishiSahu 感谢您的回复,但我说的是 MQTT 库。另外,我找到了一个库,它允许我在没有这里需要的所有缓冲区和套接字的情况下实现 MQTT。它的react_native_mqtt。但请务必使用代理的 Web 套接字端口。否则它甚至不会连接。
【解决方案2】:

您可能需要安装 nodejs 缓冲区。 (npm i -S buffer) 还要检查this answer

【讨论】:

  • 我已经尝试了这两个选项。但是当我在相应模块中添加global.Buffer = global.Buffer || require ('buffer').Buffer 时,出现以下错误:TypeError: undefined is not an object (evaluating 'process.version.slice')
猜你喜欢
  • 2018-07-04
  • 2018-05-28
  • 2018-10-26
  • 1970-01-01
  • 2021-10-10
  • 2018-05-12
  • 2017-01-24
  • 2019-05-27
  • 2021-11-13
相关资源
最近更新 更多