close

[Solved] Uncaught ReferenceError: Buffer is not defined

I am facing the following error in my nodejs application: Uncaught ReferenceError: Buffer is not defined. In this Exerror article, We are going to learn about How to reproduce this error and we will discuss All Possible Solutions Let’s Get Start With This Article.

How Uncaught ReferenceError: Buffer is not defined Error Occurs?

I am facing the following error in my nodejs application:

Uncaught ReferenceError: Buffer is not defined

So here I am writing all the possible solutions that I have tried to resolve this error.

How To Solve Uncaught ReferenceError: Buffer is not defined Error?

  1. How To Solve Uncaught ReferenceError: Buffer is not defined Error?

    To Solve Uncaught ReferenceError: Buffer is not defined Error First of All Install buffer by running this command: npm install –save buffer Then add ProvidePlugin in Your webpack.config.js Just Like This: new webpack.ProvidePlugin({ Buffer: [‘buffer’, ‘Buffer’], }), Now, You need to add resolve in your webpack.config.js Just Like This: resolve: { fallback: { “buffer”: require.resolve(“buffer”) } }, Then Import Buffer Just Like This: import { Buffer } from ‘buffer’; Now, Your error may be solved. Thanks.

  2. Uncaught ReferenceError: Buffer is not defined

    To Solve Uncaught ReferenceError: Buffer is not defined Error You need to add ProvidePlugin in webpack.config.js and then your error will be solved. First of all, open your webpack.config.js and add ProvidePlugin in the plugins section Just like This: new webpack.ProvidePlugin({ Buffer: [‘buffer’, ‘Buffer’], }), And now, Your error will be solved. Thank You.

Solution 1: Add ProvidePlugin in webpack.config.js

To Solve Uncaught ReferenceError: Buffer is not defined Error You need to add ProvidePlugin in webpack.config.js and then your error will be solved. First of all, open your webpack.config.js and add ProvidePlugin in the plugins section Just like This.

const webpack = require('webpack');

module.exports = {
...
    plugins: [
        new webpack.ProvidePlugin({
            Buffer: ['buffer', 'Buffer'],
        }),
    ],
...
}

And now, Your error will be solved. Thank You.

Solution 2: Install buffer

First of All Install buffer by running this command.

npm install --save buffer

OR 

yarn add buffer

Then add ProvidePlugin in Your webpack.config.js Just Like This.

const webpack = require('webpack');

module.exports = {
...
    plugins: [
        new webpack.ProvidePlugin({
            Buffer: ['buffer', 'Buffer'],
        }),
    ],
...
}

Now, You need to add resolve in your webpack.config.js Just Like This.

  resolve: {
        fallback: {
            "buffer": require.resolve("buffer")
        }
    },

Then Import Buffer Just Like This.

import { Buffer } from 'buffer';

// @ts-ignore
window.Buffer = Buffer;

Now, Your error may be solved. Thanks.

Conclusion

It’s all About this error. I hope We Have solved Your error. Comment below Your thoughts and your queries. Also, Comment below on which solution worked for you.

Also, Read

Leave a Comment