Buffer is not defined in React-vite

Solution 1:

Install this library

@esbuild-plugins/node-globals-polyfill

and add this in your viteConfig

optimizeDeps: {
    esbuildOptions: {
        // Node.js global to browser globalThis
        define: {
            global: 'globalThis'
        },
        // Enable esbuild polyfill plugins
        plugins: [
            NodeGlobalsPolyfillPlugin({
                buffer: true
            })
        ]
    }
}