使用MQTT的React应用程序的Webpack polyfills

pcrecxhr  于 2023-04-06  发布在  Webpack
关注(0)|答案(1)|浏览(190)

我使用create-react-app的typescript模板创建了一个新项目。我希望我的react应用订阅MQTT Broker。这就是为什么我使用了typescript mqtt库。我遵循了文档,但在启动我的react应用后,我遇到了以下问题:

ERROR in ./node_modules/mqtt/lib/connect/index.js 5:12-26
Module not found: Error: Can't resolve 'url' in '/Users/test/Code/mqtt-subscriber/node_modules/mqtt/lib/connect'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "url": require.resolve("url/") }'
    - install 'url'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "url": false }

使用的版本为:

"mqtt": "^4.3.7",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "5.0.1",

你有什么办法解决这个问题吗?我没有webpack配置文件。
如果我安装url包,我会得到其他错误:

Buffer is not defined 'url'5:12-26 Module not found
xytpbqjk

xytpbqjk1#

这看起来像this github issue-尝试从dist导入:

import mqtt from "mqtt/dist/mqtt";

或使用mqtt-precompiled

相关问题