Buffer is not defined | Vite

See original GitHub issue

Hello,

I’m facing an issue which I, after hours of trying, can not solve.

image

@walletconnect/web3-provider”: “^1.6.6”,

making use of rollup-plugin-polyfill-node as suggested in some posts, does not work. Any idea how to solve this issue?

I already make use of:

  <body>
    <!-- This is a hack to make web3 / walletconnect working with vite -->
    <script>window.global = window;</script>
    <script type="module">
        import process from "process";
        // import { Buffer } from "buffer";
        import { Buffer } from 'safe-buffer'
        import EventEmitter from "events";
        
        window.Buffer = Buffer;
        window.process = process;
        window.EventEmitter = EventEmitter;

        window.Buffer = window.Buffer || require('buffer').Buffer;

    </script>
    <div id="app" class="w-full bg-black"></div>

    <script type="module" src="/src/main.ts"></script>
  </body>

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8

github_iconTop GitHub Comments

9reactions
Nuzzletcommented, Mar 26, 2022

Hey guys. I found another solution to this problem. I simply import wallet connect via: import WalletConnectProvider from '@walletconnect/web3-provider/dist/umd/index.min.js'; and to get the types working, I made a d.ts file with: declare module '@walletconnect/web3-provider/dist/umd/index.min.js' { import WalletConnectProvider from '@walletconnect/web3-provider/dist/esm/index'; export default WalletConnectProvider }

5reactions
eikebartelscommented, Dec 8, 2021

I got it 😃

That’s how my config looks:

import * as path from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import pkg from './package.json'
import inject from '@rollup/plugin-inject'

export default defineConfig({
  server: {
    fs: {
      strict: false
    }
  },
  plugins: [
    vue({
      script: {
        refSugar: true,
      },
    }),
  ],
  optimizeDeps: {
    exclude: [
              'web3', 
            ] // <= The libraries that need shimming should be excluded from dependency optimization.
  },
  build: {
    sourcemap: true,
    commonjsOptions: {
      transformMixedEsModules: true,
    },
    rollupOptions: {
			plugins: [inject({ Buffer: ['Buffer', 'Buffer'] })],
		},
  },
  resolve: {
    alias: {
      process: "process/browser",
      stream: "stream-browserify",
      zlib: "browserify-zlib",
      util: 'util',
      '@': path.resolve(__dirname, './src'),
    },
  },
})

and my body:

<body>
    <!-- This is a hack to make web3 / walletconnect working with vite -->
    <script>window.global = window;</script>

    <div id="app" class="w-full bg-black"></div>

    <script type="module" src="/src/main.ts"></script>
  </body>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Buffer is not defined in React-vite - Stack Overflow
I try to add plugins, add define for Buffer, but it's not work. const viteConfig = defineConfig({ /* define: { "Buffer": {} },*/...
Read more >
vue3+vite reports Uncaught ReferenceError: Buffer is not ...
Buffer is used for data conversion in the vue3+vite project, and the running project reports Uncaught ReferenceError: Buffer is not defined ...
Read more >
Migrating our React setup to Vite - by Darko Atanasovski
First issues with the new setup​​ The most common errors were: Buffer or process are not defined. These functions/constants are used by the ......
Read more >
Buffer is not defined. Using Phantom Wallet, Solana and ...
I pick a working project from a friend and set mine to have the same settings. What I could say for sure is...
Read more >
vite出现global is not defined和Buffer is not defined的解决方法
在vite搭建的脚手架当中使用tron的插件tronweb的时候,会出现如下警告:. 进行交易发送的时候,会出现如下报错,并且导致交易失败:. 解决方法如下:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found