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.

image

To Reproduce

  • Check out repro-repo.
  • cd docs
  • python3 -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)

Most upvoted comments

had the same issue with storybook + next/link, fix was:

// .storybook/main.js
module.exports {
  async viteFinal(config, { configType }) {
    return mergeConfig(config, {
      define: { 'process.env': {} },
    });
  },
  // ...rest of config
}

I kinda fixed it with:

      define: {
        "process.env": {},
      },

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 process doesn’t exist in the browser, it needs to be polyfilled.

This is a very dirty workaround (.storybook/main.js):

module.exports = {
    async viteFinal(config, { configType }) {
        return mergeConfig(config, {
            define: {
                ...
                'process.env.NODE_DEBUG': false,
            },
            ...
        });
    },
    ...
};

@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

@storybook/builder-manager@npm:7.0.0-beta.31
│  └─ util@npm:0.12.5 (via npm:^0.12.4)
│
├─ @storybook/codemod@npm:7.0.0-beta.31
│  └─ util@npm:0.12.5 (via npm:^0.12.4)

There is already an upstream PR fixing this, https://github.com/browserify/node-util/pull/62, but it seemed stalled.

Vite does not polyfill process for the browser like webpack does. You’ll want to use import.meta.env instead, 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 esbuild option (https://vitejs.dev/config/shared-options.html#esbuild), though I haven’t done that before myself. You can set this in viteFinal in your .storybook/main.js as seen here: https://github.com/storybookjs/builder-vite#customize-vite-config

There’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.