9

I created a React project by running npx create-react-app my-app

I installed mqtt-react-hooks

I added the App script

import { Connector } from 'mqtt-react-hooks';

import Status from './Status';

function App() {
    return (
        <Connector
          brokerUrl="mqtt://127.0.0.1:80/"
          parserMethod={(msg) => msg} // msg is Buffer
        >
          <Status />
        </Connector>
  );
}

export default App;

I get this error in the console

image

hardillb
  • 54,545
  • 11
  • 67
  • 105
Yehuda Zadik
  • 99
  • 1
  • 3
  • 11
  • 2
    Please do not post images of text, they are hard to read, impossible to search and for people that use screen readers. Copy and Paste the actual text and then use the toolbar to format it. – hardillb Dec 20 '21 at 10:40
  • 1
    does this issue help? https://github.com/mqttjs/MQTT.js/issues/1294 – Will Jenkins Dec 20 '21 at 10:45
  • That solved my issue https://stackoverflow.com/questions/68707553/uncaught-referenceerror-buffer-is-not-defined – Yehuda Zadik Dec 20 '21 at 15:12

7 Answers7

13

As mentioned in answers here please also consider the following:

npm install --save buffer

import {Buffer} from 'buffer';

It won't help in case of external library dependency but might save you from reverting other libraries in case of using Buffer in code directly.

N Molchanov
  • 183
  • 5
3

Use this on the page or function where you get an error:

window.Buffer = window.Buffer || require("buffer").Buffer;
munal
  • 71
  • 3
2

I had this problem too.

Recently I create a new version of react app and when I used mqtt.js ( not mqtt-react-hooks ) this bad Error was shown!!!

I found out Webpack version 5 does not support Buffer and so on. Webpack 5 removes Buffer (see this info), effectively breaking MQTT library since it has explicit usages of it in the code.

so I downgrade to Webpack 4 and it's work. if you don't know how to do that, this link might be helpful. How to downgrade version of Webpack?.

2

To me downgrading react-scripts to version 4.0.3 fixed the problem. It is not a proper fix but its something...

In my case I needed to do the following also:

  1. In package.json use react-script 4.0.3
  2. Remove package-lock.json
  3. remove node_modules folder
  4. run npm install

After all this everything seems to be working fine.

Iván
  • 320
  • 3
  • 13
1

In Webpack version 5, Webpack no longer automatically polyfill's Node.js API's if they are not natively supported anymore. The browser environment does not support Buffer natively, therefore we now need to add a third party Buffer package and point Node.js to it in the Webpack config. See how to polyfill buffer with webpack 5.

Allbutone
  • 71
  • 2
  • 3
0

for me what worked was:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import inject from '@rollup/plugin-inject'

export default defineConfig({
    plugins: [vue()],
    resolve: {
        alias: {
            '@': path.resolve(__dirname, 'src'),
        }
    },
    build: {
        rollupOptions: {
            plugins: [inject({ Buffer: ['buffer', 'Buffer'] })],
        },
    },
})

reply to this comment:

https://github.com/vitejs/vite/discussions/2785#discussioncomment-1452855

0

the only solution that worked for me was this:

npm add node-stdlib-browser
npm add -D vite-plugin-node-stdlib-browser

and then:

// vite.config.js
import { defineConfig } from 'vite'
import nodePolyfills from 'vite-plugin-node-stdlib-browser'

export default defineConfig({
  // other options
  plugins: [nodePolyfills()]
})
Mariusz
  • 2,617
  • 1
  • 23
  • 26