walletconnect-monorepo icon indicating copy to clipboard operation
walletconnect-monorepo copied to clipboard

Buffer is not defined error with CRA

Open wnz99 opened this issue 2 years ago • 18 comments

The standalone client will throw this error with CRA and package "react-scripts": "^5.0.0":

image

The reason is that Webpack 5 and CRA v5 don't add fallbacks for nodejs modules any more.

wnz99 avatar Feb 06 '22 11:02 wnz99

@wnz99 I'm also running into this, have you figured out a fix?

derekcsm avatar Feb 07 '22 22:02 derekcsm

I ran into the same issue with a lot of modules missing when using the last version of CRA a few days ago, it uses webpack 5.x, that has remove some node modules, Buffer is one them, a solution for me was to use Craco to config the webpack without ejecting. https://github.com/gsoft-inc/craco.

const webpack = require('webpack');

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

ynunezaguirre avatar Feb 08 '22 18:02 ynunezaguirre

@ynunezaguirre Thanks. The Craco workarout indeed fixes the Buffer dep issue, but pops new dependency issues with other modules. Do you think I should do the same thing for other missing modules to make it work? (pretty heavy to downgrade so many packages)

hanahem avatar Feb 09 '22 12:02 hanahem

Personally, I think that the documentation should not state that the package is compatible with browser, if it depends on modules only available on nodeJS. The browser version should include anything necessary to run out of the box, imho.

wnz99 avatar Feb 09 '22 13:02 wnz99

@ynunezaguirre Thanks. The Craco workarout indeed fixes the Buffer dep issue, but pops new dependency issues with other modules. Do you think I should do the same thing for other missing modules to make it work? (pretty heavy to downgrade so many packages)

For now i haven't found another workaround to make it run :/
There is a plugin node-polyfill-webpack-plugin that maybe can help you , but doesn't work for me with all modules missing.

ynunezaguirre avatar Feb 09 '22 13:02 ynunezaguirre

Just found a dirty workaround similar to the one posted by @ynunezaguirre Not using Craco but react-app-rewired.

Still on webpack 5.x Added a config-overrides.js in the root:

const webpack = require('webpack');
module.exports = function override(config, env) {
    config.resolve.fallback = {
        url: require.resolve('url'),
        assert: require.resolve('assert'),
        buffer: require.resolve('buffer'),
    };
    config.plugins.push(
        new webpack.ProvidePlugin({
            process: 'process/browser',
            Buffer: ['buffer', 'Buffer'],
        }),
    );

    return config;
}

And npm install process

Then updated the scripts part of package.json

"scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test",
    "eject": "react-app-rewired eject"
 }

This works fine with me for now. I can use the client and pop the QR modal.

hanahem avatar Feb 09 '22 13:02 hanahem

craco config didn't work for me initially until I installed buffer as a dev dependency and did -> buffer: require.resolve('buffer/')

missing / at the end of buffer.

from the docs:

To require this module explicitly, use require('buffer/') which tells the node.js module lookup algorithm (also used by browserify) to use the npm module named buffer instead of the node.js core module named buffer!

Sednaoui avatar Feb 16 '22 10:02 Sednaoui

@esbuild-plugins/node-globals-polyfill was able to help me out with global is not defined and Buffer is not defined here is related comment https://github.com/plouc/nivo/issues/1455#issuecomment-1041830487

armgit5 avatar Feb 16 '22 16:02 armgit5

Thanks @hanahem, also got it to work now. I had to additionally install util and add it to the config npm install util

/* config-overrides.js */
const webpack = require('webpack');
module.exports = function override(config, env) {
    config.resolve.fallback = {
        util: require.resolve('util/'),
        url: require.resolve('url'),
        assert: require.resolve('assert'),
        buffer: require.resolve('buffer'),
    };
    config.plugins.push(
        new webpack.ProvidePlugin({
            process: 'process/browser',
            Buffer: ['buffer', 'Buffer'],
        }),
    );

    return config;
}

This is just terrible imo, I spent an ENTIRE day fumbling around, exploring solutions, because of incompatible packages.

willisk avatar Feb 22 '22 19:02 willisk

Are there any plans to fix this?

ddkang avatar Mar 16 '22 00:03 ddkang

Are there any plans to fix this?

I was able to make it run by downgrading react-scripts version to 4.0.3 but I don't think it's the best solution. This should be fixed ASAP @pedrouid

atropos0902 avatar Mar 16 '22 02:03 atropos0902

Yeah I wish there was a solution for this. I'm on Redwoodjs.

crazyrabbitLTC avatar Mar 19 '22 19:03 crazyrabbitLTC

Are there any plans to fix this? This bug prevents updating to the latest versions of packages.

ddkang avatar Apr 08 '22 22:04 ddkang

Here is a solution from coinbase https://docs.cloud.coinbase.com/wallet-sdk/docs/web3-react#a-nametroubleshootingatroubleshooting

ShevaShen avatar Apr 14 '22 22:04 ShevaShen

This does not work with create-react-app, which does not allow webpack overrides

ddkang avatar Apr 14 '22 22:04 ddkang

This does not work with create-react-app, which does not allow webpack overrides

downgrade to react-script v4, v5 has so many bugs the CRA team needs to fix

ShevaShen avatar Apr 21 '22 23:04 ShevaShen

If we are using React 17 or 18, we can't avoid "Buffer undefined" proflem without "config-overides.js". We have to downgrade react version to use walletconnect libraries.

ericlewis966 avatar Jun 17 '22 01:06 ericlewis966

npm install buffer
window.Buffer = window.Buffer || require("buffer").Buffer;

function App() {
...
}

OmBayus avatar Jul 07 '22 20:07 OmBayus

I ran into the same issue with a lot of modules missing when using the last version of CRA a few days ago, it uses webpack 5.x, that has remove some node modules, Buffer is one them, a solution for me was to use Craco to config the webpack without ejecting. https://github.com/gsoft-inc/craco.

const webpack = require('webpack');

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

Slight change (needs to be nested in the webpack property) to get this working with CRACO, and CRA 5.0.1

const webpack = require('webpack');
module.exports = {
    webpack: {
        configure: {
            resolve: {
                fallback: {
                    buffer: require.resolve('buffer'),
                },
            },
        },
        plugins: {
            add: [
                new webpack.ProvidePlugin({
                    Buffer: ['buffer', 'Buffer'],
                }),
            ],
        },
    },
};

DalSoft avatar Oct 18 '22 14:10 DalSoft

npm install buffer
window.Buffer = window.Buffer || require("buffer").Buffer;

function App() {
...
}

This worked for me.

ogbuanu avatar Nov 24 '22 13:11 ogbuanu

@wnz99 is this still an issue?

finessevanes avatar Jan 13 '23 04:01 finessevanes

No

On Fri, Jan 13, 2023, 5:34 AM vanes @.***> wrote:

@wnz99 https://github.com/wnz99 is this still an issue?

— Reply to this email directly, view it on GitHub https://github.com/WalletConnect/walletconnect-monorepo/issues/748#issuecomment-1381305253, or unsubscribe https://github.com/notifications/unsubscribe-auth/ATMQYG7UF6P76MCXT5Q43WDWSDLOLANCNFSM5NVHDNNA . You are receiving this because you commented.Message ID: @.***>

ogbuanu avatar Jan 13 '23 05:01 ogbuanu

I still have this error just installing buffer and doing window.Buffer = window.Buffer || Buffer;, in order to fix is it necessary to use CRACO?

RicFer01 avatar Mar 15 '23 12:03 RicFer01

I continue to get this error, and am using

  util: require.resolve('util/'),
        assert: require.resolve('assert/'),
        stream: require.resolve('stream-browserify'),
        zlib: require.resolve('browserify-zlib'),
        buffer: require.resolve('buffer'),
        process: require.resolve('process/browser'),

as well as adding the plugins via:

plugins: {
      add: [
        new webpack.ProvidePlugin({
          Buffer: ['buffer', 'Buffer'],
          process: 'process/browser',
        }),
      ],
    },

I also added window.Buffer = window.Buffer || Buffer; to the root index.js file

guppykang avatar May 24 '23 22:05 guppykang

I continue to get this error, and am using

  util: require.resolve('util/'),
        assert: require.resolve('assert/'),
        stream: require.resolve('stream-browserify'),
        zlib: require.resolve('browserify-zlib'),
        buffer: require.resolve('buffer'),
        process: require.resolve('process/browser'),

as well as adding the plugins via:

plugins: {
      add: [
        new webpack.ProvidePlugin({
          Buffer: ['buffer', 'Buffer'],
          process: 'process/browser',
        }),
      ],
    },

I also added window.Buffer = window.Buffer || Buffer; to the root index.js file

I fixed like this:

config.resolve.fallback = {
	crypto: require.resolve('crypto-browserify'),
  	util: require.resolve('util/'),
  	url: require.resolve('url'),
  	assert: require.resolve('assert'),
  	buffer: require.resolve('buffer'),
};

config.plugins.push(
	new webpack.ProvidePlugin({
    	process: 'process/browser',
    	Buffer: ['buffer', 'Buffer'],
	}),
);

RicFer01 avatar May 29 '23 09:05 RicFer01