storybook: [@storybook/builder-vite] `process is not defined` error in production bundle.
Describe the bug
I am using @storybook/builder-vite and started getting process is not defined only in the production build. The preview still runs ok. When I remove builder: '@storybook/builder-vite', the build gets fixed.

To Reproduce
- Check out repro-repo.
cd docspython3 -m http.server 8000
System System: OS: Linux 5.15 Ubuntu 20.04.3 LTS (Focal Fossa) CPU: (8) x64 Intel® Core™ i7-1065G7 CPU @ 1.30GHz Binaries: Node: 16.15.1 - ~/.nvm/versions/node/v16.15.1/bin/node npm: 8.13.2 - ~/.nvm/versions/node/v16.15.1/bin/npm
Additional context
"devDependencies": {
"@fluentui/react-theme": "^9.0.0",
"@griffel/react": "^1.3.0",
"@storybook/addon-actions": "^6.5.10",
"@storybook/addon-essentials": "^6.5.10",
"@storybook/addon-interactions": "^6.5.10",
"@storybook/addon-links": "^6.5.10",
"@storybook/builder-vite": "^0.2.2",
"@storybook/jest": "^0.0.10",
"@storybook/react": "^6.5.10",
"@storybook/testing-library": "^0.0.13",
"assert": "^2.0.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"typescript": "^4.7.4",
"vite": "^3.0.5"
}
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 12
- Comments: 21 (8 by maintainers)
had the same issue with storybook +
next/link, fix was:I kinda fixed it with:
in my viteFinal config. I’m mixing Vite due to my design-system is being built with esbuild. Therefore I used vite to display the stories from the design system. However, I also have some specific storybook components in the Nextjs application that didn’t work. So it was originally a problem due to Nextjs using webpack.
I did this and it worked - for now. What seems to be causing the error is the fact the we are using webpack 5 under the hood (?), and webpack 5 doesnt provide polyfills by default anymore. Since
processdoesn’t exist in the browser, it needs to be polyfilled.This is a very dirty workaround (
.storybook/main.js):@jaredh159 that will probably work, but keep in mind that Next.js is built on top of Webpack, so we don’t recommend using the Vite builder for your Storybook, since you can run into inconsistencies like this.
I also encountered this error. For me, it was caused by https://github.com/browserify/node-util/blob/ef984721db7150f651800e051de4314c9517d42c/util.js#L109-L116 with util being pulled-in by
There is already an upstream PR fixing this, https://github.com/browserify/node-util/pull/62, but it seemed stalled.
Vite does not polyfill
processfor the browser like webpack does. You’ll want to useimport.meta.envinstead, for code that runs in the browser. See https://vitejs.dev/guide/env-and-mode.html#env-variables.If you want to add esbuild plugins, I believe you can use the
esbuildoption (https://vitejs.dev/config/shared-options.html#esbuild), though I haven’t done that before myself. You can set this inviteFinalin your.storybook/main.jsas seen here: https://github.com/storybookjs/builder-vite#customize-vite-configThere’s a thread in https://github.com/browserify/node-util/issues/43 that discusses solutions to this problem in more detail. I was able to find a more general solution in another thread, in this comment.