在vite最新版本中使用web3会报错只需要在vite.config.ts添加如下代码即可解决报错
import { fileURLToPath, URL } from 'node:url' import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' //引入 import { resolve } from 'path' export default defineConfig({ plugins: [ vue(), ], resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)), //添加如下代码 web3: resolve(__dirname, './node_modules/web3/dist/web3.min.js') } }, })
如果是js同理,如果还有问题在index.html加上如下代码
<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>
标签:process,js,web3,window,报错,import,vite From: https://www.cnblogs.com/nanchengwuqiu/p/17487718.html