node.jssveltevitesveltekit

Uncaught (in promise) ReferenceError: Buffer is not defined in vite/sveltekit with Torus


I need a way to alias Buffer for the browser.

Not sure which module requires this but its probably crypto related.

500

Buffer is not defined

@http://localhost:3000/_app/pages/index.svelte-2478ade3.js:1:111754

Problem is using Torus node library.

Here is a repo: https://github.com/ralyodio/torus-bufer

npm i
npm run build
npm run preview

...you will see error. npm run dev works fine since node handles requests. Its the browser only environment that is the issue.

The polyfill in svelte.config.js appears to not work: https://github.com/ralyodio/torus-bufer/blob/master/svelte.config.js

Update: so it appears the bigger issue is that NodeGlobalsPolyfillPlugin is not respected when doing npm run build with sveltekit static adapter.


Solution

  • I fixed it by adding a vite alias to the es5 version of toruslabs openlogin sdk:

                resolve: {
                    alias: {
                        '@toruslabs/openlogin': path.resolve(
                            './node_modules/@toruslabs/openlogin/dist/openlogin.umd.min.js'
                        )
                    }
                }
    

    I no longer get the "Buffer is undefined" error on static builds.