storybook: [Bug]: Using material ui with storybooks results with "_interopRequireDefault2 is not a function" error.

Describe the bug

I decided to try storybook with react and vite and it looks like it does not work with material ui.

Importing any component (for example Button) results with the following error:

image
TypeError: _interopRequireDefault2 is not a function
    at node_modules/@mui/system/colorManipulator.js (https://gqairkxxypgithub-v51r--6006--70dbe416.local-credentialless.webcontainer.io/node_modules/.cache/storybook/1c3385a5d25e538d10b518b310c74d3ca2690b6aaffeadccd74da79736171f86/sb-vite/deps/@mui_material.js?v=a14f6c47:2995:35)
    at __require2 (https://gqairkxxypgithub-v51r--6006--70dbe416.local-credentialless.webcontainer.io/node_modules/.cache/storybook/1c3385a5d25e538d10b518b310c74d3ca2690b6aaffeadccd74da79736171f86/sb-vite/deps/chunk-LK32TJAX.js?v=a14f6c47:18:50)
    at https://gqairkxxypgithub-v51r--6006--70dbe416.local-credentialless.webcontainer.io/node_modules/.cache/storybook/1c3385a5d25e538d10b518b310c74d3ca2690b6aaffeadccd74da79736171f86/sb-vite/deps/@mui_material.js?v=a14f6c47:10557:39

Example code:

MuiButton.tsx

import { Button } from '@mui/material';

export const MuiButton = () => {
  return <Button>Click me</Button>;
};

MuiButton.stories.tsx

import type { Meta, StoryObj } from '@storybook/react';
import { fn } from '@storybook/test';
import { MuiButton } from './MuiButton';

// More on how to set up stories at: https://storybook.js.org/docs/writing-stories#default-export
const meta = {
  title: 'Example/MuiButton',
  component: MuiButton,
  parameters: {
    // Optional parameter to center the component in the Canvas. More info: https://storybook.js.org/docs/configure/story-layout
    layout: 'centered',
  },
  // This component will have an automatically generated Autodocs entry: https://storybook.js.org/docs/writing-docs/autodocs
  tags: ['autodocs'],
  // More on argTypes: https://storybook.js.org/docs/api/argtypes
  argTypes: {
    // backgroundColor: { control: 'color' },
  },
  // Use `fn` to spy on the onClick arg, which will appear in the actions panel once invoked: https://storybook.js.org/docs/essentials/actions#action-args
  args: { onClick: fn() },
} satisfies Meta<typeof MuiButton>;

export default meta;
type Story = StoryObj<typeof meta>;

// More on writing stories with args: https://storybook.js.org/docs/writing-stories/args
export const Primary: Story = {
  args: {},
};

Reproduction link

https://stackblitz.com/edit/github-rxrfuy-2iqxep?file=src%2Fstories%2FMuiButton.stories.ts

Reproduction steps

  1. Use a component from @mui/material package, for example Button inside a story.
  2. Start storybook, open story and see the error

System

Storybook Environment Info:

  System:
    OS: Linux 5.0 undefined
    CPU: (6) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm <----- active
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @storybook/addon-essentials: ^8.3.0-alpha.0 => 8.3.0-alpha.0 
    @storybook/addon-interactions: ^8.3.0-alpha.0 => 8.3.0-alpha.0 
    @storybook/addon-links: ^8.3.0-alpha.0 => 8.3.0-alpha.0 
    @storybook/addon-onboarding: ^8.3.0-alpha.0 => 8.3.0-alpha.0 
    @storybook/blocks: ^8.3.0-alpha.0 => 8.3.0-alpha.0 
    @storybook/react: ^8.3.0-alpha.0 => 8.3.0-alpha.0 
    @storybook/react-vite: ^8.3.0-alpha.0 => 8.3.0-alpha.0 
    @storybook/test: ^8.3.0-alpha.0 => 8.3.0-alpha.0 
    storybook: ^8.3.0-alpha.0 => 8.3.0-alpha.0

Additional context

It looks like 5.15.10 is the last version of mui that works with storybook correctly, but it’s 5 months old now.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 15
  • Comments: 17 (4 by maintainers)

Commits related to this issue

Most upvoted comments

OK, I found the problem. The fix will take a minute, because it’s Friday evening.

To resolve the _interopRequireDefault2 is not a function error when using Material UI with Storybook and Vite, follow these steps:

  1. Update Vite Configuration: Ensure your Vite configuration properly handles CommonJS modules. Update your vite.config.js to include the vite-plugin-commonjs plugin.
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import commonjs from 'vite-plugin-commonjs';

export default defineConfig({
  plugins: [react(), commonjs()]
});
  1. Update Storybook Configuration: Ensure your Storybook configuration is set up to use Vite. Update your .storybook/main.js to include the @storybook/react-vite framework.
module.exports = {
  framework: '@storybook/react-vite',
  stories: ['../src/**/*.stories.@(js|jsx|ts|tsx)'],
  addons: [
    '@storybook/addon-links',
    '@storybook/addon-essentials',
    '@storybook/addon-interactions',
  ],
};
  1. Install Dependencies: Ensure you have the necessary dependencies installed.
npm install vite-plugin-commonjs @storybook/react-vite

These steps should help resolve the issue with Material UI components in Storybook using Vite.

References

/code/lib/blocks/src/examples/Button.stories.tsx /code/renderers/web-components/template/cli/ts-4-9/Button.stories.ts /docs/get-started/frameworks/react-vite.mdx

About Greptile

This response provides a starting point for your research, not a precise solution.

Help us improve! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

Ask Greptile · Edit Issue Bot Settings

Hi, I’m also having the same issue with storybook 8.2.1

To resolve the _interopRequireDefault2 is not a function error when using Material UI with Storybook and Vite, follow these steps:

1. **Update Vite Configuration**: Ensure your Vite configuration properly handles CommonJS modules. Update your `vite.config.js` to include the `vite-plugin-commonjs` plugin.
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import commonjs from 'vite-plugin-commonjs';

export default defineConfig({
  plugins: [react(), commonjs()]
});
2. **Update Storybook Configuration**: Ensure your Storybook configuration is set up to use Vite. Update your `.storybook/main.js` to include the `@storybook/react-vite` framework.
module.exports = {
  framework: '@storybook/react-vite',
  stories: ['../src/**/*.stories.@(js|jsx|ts|tsx)'],
  addons: [
    '@storybook/addon-links',
    '@storybook/addon-essentials',
    '@storybook/addon-interactions',
  ],
};
3. **Install Dependencies**: Ensure you have the necessary dependencies installed.
npm install vite-plugin-commonjs @storybook/react-vite

These steps should help resolve the issue with Material UI components in Storybook using Vite.

References

/code/lib/blocks/src/examples/Button.stories.tsx /code/renderers/web-components/template/cli/ts-4-9/Button.stories.ts /docs/get-started/frameworks/react-vite.mdx

About Greptile

This is massive bs, sorry.

I am having the same issue, storybook v8.2.1, MUI v5.16.0.

Hi @valentinpalkovic, it does work with Storybook 8.1.11