Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MQTT.js does not work with Webpack 5.x #1582

Closed
massi-ang opened this issue Mar 6, 2023 · 13 comments
Closed

MQTT.js does not work with Webpack 5.x #1582

massi-ang opened this issue Mar 6, 2023 · 13 comments

Comments

@massi-ang
Copy link

massi-ang commented Mar 6, 2023

Webpack 5.x removed the Nodejs polyfills (see facebook/create-react-app#12072), including Buffer. Since MQTTjs depends on mqtt-packets which uses Buffer it cannot be used with Webpack 5.x following the instructions provided in the doc.

Please fix removing dependency on Buffer to make the library compatible with Webpack 5.x without requiring polyfills, or update the documentation accordingly.

@viliket
Copy link

viliket commented Mar 10, 2023

You can use the following workaround (tested to work with the latest react-scripts 5.0.1 used in create-react-app) when using Webpack >= 5 which no longer provides the Node.js polyfills needed by MQTT.js:

  1. Install the buffer and process as normal dependencies:
    npm i buffer process
    
  2. Create a new file like mqtt-patch.ts with the following contents:
    import buffer from 'buffer';
    import process from 'process';
    
    // Hack to get mqtt package work with Webpack 5
    (window as any).Buffer = buffer.Buffer;
    (window as any).process = process;
    
    export {};
    
  3. Import the above file before anything else inside your "root" module (in Create React App index.tsx):
    import './mqtt-patch'; // First import
    // Other imports as usual
    
  4. After this you should be able to use MQTT.js as usual with Webpack >= 5.

@Nosferatu31
Copy link

/mqtt/lib/connect/index.js also depends on url which has to be polyfilled

@MarcinSzalomski
Copy link

MarcinSzalomski commented May 4, 2023

The alternative to the answer of @viliket would be to load the polyfills via webpack instead of importing them from a file.
Additionaly, mqtt suggests the webpack alternative as we can see in the console prompt.

image

  1. Install buffer, process and url (since it also needs to be polyfilled - just like @Nosferatu31 suggested).
    npm install buffer process url

  2. Edit your webpack.config.js with following snippets

module.exports = {
   ...
   plugins: [
      new webpack.ProvidePlugin({ Buffer: ['buffer', 'Buffer'] }),
      new webpack.ProvidePlugin({ process: 'process' }),
      new webpack.ProvidePlugin({ url: 'url' }),
   ]
   ...
   resolve: {
      fallback: {
         buffer: require.resolve('buffer/'),
         url: require.resolve('url/'),
         process: require.resolve('process/'),
      }
   }
}

@zarte
Copy link

zarte commented May 6, 2023

You can use the following workaround (tested to work with the latest react-scripts 5.0.1 used in create-react-app) when using Webpack >= 5 which no longer provides the Node.js polyfills needed by MQTT.js:

  1. Install the buffer and process as normal dependencies:
    npm i buffer process
    
  2. Create a new file like mqtt-patch.ts with the following contents:
    import buffer from 'buffer';
    import process from 'process';
    
    // Hack to get mqtt package work with Webpack 5
    (window as any).Buffer = buffer.Buffer;
    (window as any).process = process;
    
    export {};
    
  3. Import the above file before anything else inside your "root" module (in Create React App index.tsx):
    import './mqtt-patch'; // First import
    // Other imports as usual
    
  4. After this you should be able to use MQTT.js as usual with Webpack >= 5.

cannot read properties of undefined (reading 'Buffer')
had npm i buffer

@robertsLando
Copy link
Member

Please consider opening a PR to fix this issue

@robertsLando
Copy link
Member

MQTT 5.0.0 BETA is now available! Try it out and give us feedback: npm i mqtt@beta. It may fix your issues

@Nosferatu31
Copy link

Nosferatu31 commented Jul 25, 2023

@robertsLando I get a:

Uncaught ReferenceError: process is not defined

js client.ts:42
factory react refresh:6

@robertsLando
Copy link
Member

robertsLando commented Jul 25, 2023

@Nosferatu31 https://stackoverflow.com/questions/41359504/webpack-bundle-js-uncaught-referenceerror-process-is-not-defined ?

Also instead of importing mqtt module you could try importing the minified version: import mqtt from 'mqtt/dist/mqtt.min'

@Nosferatu31
Copy link

Since I am using CRA, I did not want to eject it to configure the Webpack configuration file...

@dvkam
Copy link

dvkam commented Jul 25, 2023

CRA is de facto not maintaned anymore.

See for context, the React documentation removed CRA as it is not longer recommended to start a react project this way and some reasoning behind this to read here reactjs/react.dev#5487 (comment)
It is completely removed from the docs.

If you are keen to move to a similar setup I can recommend the React Vite template. Depending on how big your project is the switch can be pretty straightforward. And there you just configure the vite config.

But if you want to stay with CRA and do not want to eject, I am not sure if there is a solution. (my guess is no)

@robertsLando
Copy link
Member

I have fixed Browser docs by adding webpack and vite setup. Check them out

@robertsLando
Copy link
Member

FYI starting from 5.2.1 you can now import mqtt in browser using:

import mqtt from 'mqtt'

@Nosferatu31
Copy link

@robertsLando I think that my issue with CRA was solved with 5.2.1, thank you!

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

No branches or pull requests

7 participants