Skip to content

Conversation

@flyfishzy
Copy link
Contributor

Background

Buffer is an API in Node.js and does not exist in browsers. When upgrading to webpack 5, webpack will no longer automatically bundles core node modules (buffer/process etc.). Which will cause the Buffer is not defined problems.
mqttjs/MQTT.js#1412
mqttjs/MQTT.js#1338

Solutions

  1. Although we can add polyfills by modifying the webpack.config.js file. But there are also some scenarios where the webpack.config.js cannot be modified (such as the Openharmony development environment I‘m using).
  2. The Node.js doc recommends to explicitly import or require 'buffer':

While the Buffer class is available within the global scope, it is still recommended to explicitly reference it via an import or require statement.
https://nodejs.org/api/buffer.html
image
After that, just insall 'buffer' can fix this problem.

npm install buffer

@ralrom
Copy link

ralrom commented Apr 19, 2022

Shouldn't the mqtt-packet dependency also be updated to v8.0.0?

Otherwise we still have the same problem when trying to use MQTT.js in the browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants