davidmyersdev / vite-plugin-node-polyfills

A Vite plugin to polyfill Node's Core Modules for browser environments.
MIT License
263 stars 17 forks source link

Uncaught ReferenceError: global is not defined #39

Closed metahubwallet closed 8 months ago

metahubwallet commented 10 months ago
image

vue: 3.3.4 vite: 4.4.9

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
import { setAutoImport } from './settings/plugins/autoImport';
import { setAutoComponents } from './settings/plugins/components';

export default defineConfig({
    plugins: [
        vue(),
        nodePolyfills({
            include: ['buffer', 'process'],
            globals: {
                Buffer: true, // can also be 'build', 'dev', or false
                global: true,
                process: true,
            },
            protocolImports: false,
        }),
        setAutoImport(),
        setAutoComponents(),
        viteCompression(), // { disable: !isProduction }
    ],
...
});
davidmyersdev commented 10 months ago

Hey there, @metahubwallet. I appreciate the config, but can you please add a reproducible example on either StackBlitz or CodeSandbox? Thanks!

paulbse commented 8 months ago
`<script>
  if (global === undefined) {
    var global = window;
  }
</script>`<script>
  if (global === undefined) {
    var global = window;
  }
</script>`
Just add this to your index.html file in your src folder
davidmyersdev commented 8 months ago

@paulbse the example you provided would expectedly error since .html files are not processed by these plugins. If you instead put the script in a .js or .ts file and imported it, it would likely work as expected.