Search code examples
javascriptsveltevitesveltekit

'Buffer' is not exported by __vite-browser-external:buffer


I'm getting this build error with vite and sveltekit using adapter-node

I'm not sure why it won't build since it relies on node to server the client.

dev works fine

'Buffer' is not exported by __vite-browser-external:buffer

I tried polyfills but they don't work.

            optimizeDeps: {
                esbuildOptions: {
                    // Node.js global to browser globalThis
                    define: {
                        global: 'globalThis'
                    },
                    // Enable esbuild polyfill plugins
                    plugins: [
                        NodeGlobalsPolyfillPlugin({
                            process: true,
                            buffer: true,
                            webworkers: true,
                        }),
                        NodeModulesPolyfillPlugin()
                    ]
                }
            },
            build: {
                minify: true,
                rollupOptions: {
                    plugins: [
                        // Enable rollup polyfills plugin
                        // used during production bundling
                        rollupNodePolyFill()
                    ]
                }
            }

Solution

  • build: {
            rollupOptions: {
                plugins: [inject({ Buffer: ['Buffer', 'Buffer'] })],
            },
        },
    

    This works with npm i -D buffer