Storybook for React with Webpack
Storybook for React & Webpack is a framework that makes it easy to develop and test UI components in isolation for React applications built with Webpack.
We recommend using @storybook/react-vite for most React projects. The Vite-based framework is faster, more modern, and offers better support for testing features.
Use this Webpack-based framework (@storybook/react-webpack5) only if you need specific Webpack features not available in Vite.
Install
To install Storybook in an existing React project, run this command in your project's root directory:
npm create storybook@latestYou can then get started writing stories, running tests and documenting your components. For more control over the installation process, refer to the installation guide.
Requirements
React ≥ 16.8
Webpack 5
Run Storybook
To run Storybook for a particular project, run the following:
npm run storybookTo build Storybook, run:
npm run build-storybookYou will find the output in the configured outputDir (default is storybook-static).
Configure
Create React App (CRA)
Support for Create React App is handled by @storybook/preset-create-react-app.
This preset enables support for all CRA features, including Sass/SCSS and TypeScript.
Manually initialized apps
If you're working on an app that was initialized manually (i.e., without the use of CRA), ensure that your app has react-dom included as a dependency. Failing to do so can lead to unforeseen issues with Storybook and your project.
FAQ
How do I manually install the React Webpack framework?
First, install the framework:
npm install --save-dev @storybook/react-webpack5Next, install and register your appropriate compiler addon, depending on whether you're using SWC (recommended) or Babel:
If your project is using Create React App, you can skip this step.
npx storybook@latest add @storybook/addon-webpack5-compiler-swcor
npx storybook@latest add @storybook/addon-webpack5-compiler-babelMore details can be found in the Webpack builder docs.
Finally, update your .storybook/main.js|ts to change the framework property:
import type { StorybookConfig } from '@storybook/react-webpack5';
const config: StorybookConfig = {
// ...
framework: '@storybook/react-webpack5', // 👈 Add this
};
export default config;How do I migrate to the React Vite framework?
Please refer to the migration instructions for @storybook/react-vite.
API
Options
You can pass an options object for additional configuration if needed:
import type { StorybookConfig } from '@storybook/react-webpack5';
const config: StorybookConfig = {
framework: {
name: '@storybook/react-webpack5',
options: {
// ...
},
},
};
export default config;builder
Type: Record<string, any>
Configure options for the framework's builder. For this framework, available options can be found in the Webpack builder docs.
