vite: vite use web3.js throw Uncaught ReferenceError: process is not defined

To solve the problem "global is not defined "

import { defineConfig, UserConfigExport, ConfigEnv } from 'vite' import vue from '@vitejs/plugin-vue' import path from 'path'; import polyfillNode from 'rollup-plugin-polyfill-node' export default defineConfig({ plugins: [ vue(), polyfillNode() ], optimizeDeps: { exclude: ['dragula'] } }) and then throw Uncaught ReferenceError: process is not defined, problem . so How can I fix it? thanks! image

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 21 (5 by maintainers)

Most upvoted comments

added to head html file

<script>window.global = window;</script>
<script type="module">
    import process from "process";
    import { Buffer } from "buffer";
    import EventEmitter from "events";
    
    window.Buffer = Buffer;
    window.process = process;
    window.EventEmitter = EventEmitter;
</script>

vite.config.ts

import vue from '@vitejs/plugin-vue'

export default {
  resolve: {
    alias: {
      process: "process/browser",
      stream: "stream-browserify",
      zlib: "browserify-zlib",
      util: 'util'
    }
  },
  plugins: [
    vue(),
  ]
}

add these dependencies browserify-zlib, events, process, stream-browserify, util

have created a pull request to your reproduction showing working dev and build for me

You are so cool.