withStorybook function wraps your Metro configuration to enable Storybook support in React Native projects using Metro bundler.
Basic Usage
metro.config.js
Configuration Options
Options
The path to the Storybook config folder where your
main.ts and preview.ts files are located.WebSocket configuration for syncing Storybook instances or sending events. When provided, creates a WebSocket server for real-time communication.Set to See WebSocket Configuration for details.
'auto' to use port 7007 with auto-detected host.Whether to use JavaScript files for Storybook configuration instead of TypeScript.When
true, generates storybook.requires.js instead of storybook.requires.ts.If
false, attempts to remove Storybook modules from the JavaScript bundle to reduce bundle size.Useful for disabling Storybook in production builds.Whether to include doc tools in the
storybook.requires file.Doc tools provide additional documentation features like component metadata extraction.Whether to use lite mode for Storybook.In lite mode, the default Storybook UI is mocked out so you don’t need to install all its dependencies like
react-native-reanimated, react-native-gesture-handler, etc.This is useful for reducing bundle size and dependencies.Whether to enable MCP (Model Context Protocol) server support.When enabled, adds an
/mcp endpoint to the channel server, allowing AI agents (Claude Code, Cursor, etc.) to query component documentation.If WebSockets are disabled, MCP documentation tools still work but story selection is unavailable.This is an experimental feature. The MCP endpoint runs on the same port as the WebSocket server (default 7007).
Examples
Full Configuration
metro.config.js
Production Build with Storybook Disabled
metro.config.js
Lite Mode for Smaller Bundle
metro.config.js
How It Works
ThewithStorybook function modifies your Metro configuration:
- Enables
require.context: Setsunstable_allowRequireContext: truefor dynamic story imports - Generates story manifest: Automatically creates
storybook.requires.tsfile with all your stories - Starts WebSocket server: Optionally creates a WebSocket server for remote control (when
websocketsoption is provided) - Resolves Storybook modules: Configures Metro to properly resolve Storybook package exports
- Strips Storybook in production: When
enabled: false, removes Storybook from the bundle
The
withStorybook function wraps your existing Metro config, so any custom configuration you have will be preserved.