Uncaught ReferenceError: global is not defined when served from vite dev server #5912
-
After trying out storybook in my project (not possible to share the code) my browser crashes when using the
When running the full build ( Tracing back the imports I find that the addition of "@storybook/react": "^6.4.1" to my packages.json somehow caused has-symbols version 1.0.1 to be imported*. which include this top-level code: Digging further I find that this has been fixed in 1.0.2 by the has-symbols maintainer after quite some resistance:
and
When searching for this issue I found at least a couple other libraries which caused the same issue. Some of the vite github issues recommend simply shimming global to window in index.html, but this feels quite wrong to me. Is this the preferred solution? The has-symbols maintainer sure seems to think this is the responsibility of the bundler and since the full (bundled) vite build work, I guess vite agree? ** It's a bit worrying that such large differences can happen between the full and development build. And if it's semi-common for libraries to (rightly?) rely on the bunder to resolve this nodejs - browser compatibility thing, isn't it a problem worth addressing? Or at least documenting the best solution. The only reference I find is this which say what not do to without further elaboration. It would have saved me a couple hours debugging at least :) Is it possible to force certain modules to be prebundled? * I gave up finding the exact causal link in my yarn.lock file 😵💫 (if anyone know of good tooling which make such digging easier I'm all ears) ** I guess it could also be due to some optimization/code splitting which cause the "offending" library to not be imported |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 21 replies
-
please open a issue at storybook regarding that outdated dependency |
Beta Was this translation helpful? Give feedback.
-
For anyone landing here, I got it fixed by adding this to my vite.config.ts export default defineConfig({
plugins: ...
esbuild: ...
define: {
// By default, Vite doesn't include shims for NodeJS/
// necessary for segment analytics lib to work
global: {},
},
}) |
Beta Was this translation helpful? Give feedback.
-
The following import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import viteTsconfigPaths from "vite-tsconfig-paths";
import svgrPlugin from "vite-plugin-svgr";
// https://vitejs.dev/config/
export default defineConfig({
// https://github.com/vitejs/vite/issues/1973
define: {
// "process.env": process.env,
// // By default, Vite doesn't include shims for NodeJS/
// // necessary for segment analytics lib to work
"global": {},
},
resolve: {
alias: {
process: "process/browser",
buffer: "buffer",
crypto: "crypto-browserify",
stream: "stream-browserify",
assert: "assert",
http: "stream-http",
https: "https-browserify",
os: "os-browserify",
url: "url",
util: "util",
},
},
plugins: [react(), viteTsconfigPaths(), svgrPlugin()],
}); |
Beta Was this translation helpful? Give feedback.
-
for React.js i've added the following to my index.html
|
Beta Was this translation helpful? Give feedback.
-
Got this from someone else, but this works as well. The problem I had was that not having
|
Beta Was this translation helpful? Give feedback.
-
Sometimes we have 2 errors related to varialble
When we encountered these 2 errors in vite.js, we can resolve by:
|
Beta Was this translation helpful? Give feedback.
-
I have defined global to 'globalThis' and after that it works fine
|
Beta Was this translation helpful? Give feedback.
-
simpl solutions for module resolve: {
alias: {
"socket.io-client": "socket.io-client/dist/socket.io.js",
},
}, |
Beta Was this translation helpful? Give feedback.
-
A note to all using |
Beta Was this translation helpful? Give feedback.
global
is a node builtin which libraries should not depend onglobal
if you have dependencies which are not browser friendlyplease open a issue at storybook regarding that outdated dependency