Skip to main content
Storybook for React Native provides a CLI tool for generating the storybook.requires file that contains your story imports.

sb-rn-get-stories

Generates the storybook.requires file by scanning your configured stories and creating the necessary imports.

Usage

sb-rn-get-stories [options]

Options

-c, --config-path
string
default:"./.rnstorybook"
The path to your config folder relative to your project directory.This folder should contain your main.ts and preview.ts files.
sb-rn-get-stories --config-path ./.storybook
-j, --use-js
boolean
default:"false"
Use a JavaScript file for storybook.requires instead of TypeScript.When enabled, generates storybook.requires.js instead of storybook.requires.ts.
sb-rn-get-stories --use-js
-D, --no-doc-tools
boolean
default:"false"
Do not include doc tools in the storybook.requires file.Doc tools provide additional documentation features like component metadata extraction.
sb-rn-get-stories --no-doc-tools
-w, --host
string
default:"undefined"
Host for WebSocket connections.The value 'auto' tells Storybook to use your local IP address.
sb-rn-get-stories --host auto
sb-rn-get-stories --host 192.168.1.100
When --host is provided, the WebSocket configuration is injected into the generated storybook.requires file.
-p, --port
number
default:"undefined"
Port for WebSocket connections.
sb-rn-get-stories --port 7007
Must be a valid port number (integer).
The --port option must be used with --host to enable WebSocket configuration.

Examples

Basic Generation

Generate the requires file with default settings:
sb-rn-get-stories
This creates .rnstorybook/storybook.requires.ts by scanning the stories configured in .rnstorybook/main.ts.

Custom Config Path

Use a different config directory:
sb-rn-get-stories --config-path ./.storybook

JavaScript Output

Generate a JavaScript file instead of TypeScript:
sb-rn-get-stories --use-js
Creates .rnstorybook/storybook.requires.js.

With WebSockets

Generate with WebSocket configuration:
sb-rn-get-stories --host auto --port 7007
The generated file will include:
globalThis.STORYBOOK_WEBSOCKET = {
  host: '192.168.1.100', // Your local IP
  port: 7007,
};

Without Doc Tools

Exclude doc tools to reduce bundle size:
sb-rn-get-stories --no-doc-tools

Complete Example

sb-rn-get-stories \
  --config-path ./.storybook \
  --use-js \
  --host auto \
  --port 7007

Generated File Structure

The storybook.requires file contains:
  1. WebSocket Configuration (if provided):
    globalThis.STORYBOOK_WEBSOCKET = {
      host: 'localhost',
      port: 7007,
    };
    
  2. Story Imports:
    const getStories = () => {
      return {
        './components/Button.stories.tsx': require('../components/Button.stories.tsx'),
        './components/Input.stories.tsx': require('../components/Input.stories.tsx'),
      };
    };
    
  3. Configuration Exports:
    export { getStories };
    

Integration with Metro

You typically don’t need to run this command manually. The withStorybook Metro wrapper automatically generates the file during bundling.
The CLI is useful for:
  • CI/CD pipelines: Pre-generate the requires file before building
  • Version control: Commit the generated file to track changes
  • Debugging: Manually regenerate the file to troubleshoot issues

Manual Regeneration

If you need to manually regenerate the file:
# Using npm
npm run sb-rn-get-stories

# Using yarn
yarn sb-rn-get-stories

# Using pnpm
pnpm sb-rn-get-stories
Add a script to your package.json:
package.json
{
  "scripts": {
    "storybook-generate": "sb-rn-get-stories"
  }
}

npm Scripts

Typical npm scripts for Storybook development:
package.json
{
  "scripts": {
    "storybook": "sb-rn-get-stories --host auto --port 7007 && expo start",
    "storybook-generate": "sb-rn-get-stories",
    "build:storybook": "sb-rn-get-stories --no-doc-tools && expo build"
  }
}

Error Handling

Invalid Port Number

If you provide an invalid port:
sb-rn-get-stories --port invalid
Error:
Invalid port number: "invalid"
Port must be a valid integer between 1 and 65535.

Missing Config Directory

If the config path doesn’t exist:
sb-rn-get-stories --config-path ./.nonexistent
The command will fail with an error indicating the directory was not found.

No Stories Found

If no stories match your configuration:
sb-rn-get-stories
Error:
No stories found
Check your main.ts stories configuration to ensure the globs match your story files.

Programmatic API

You can also use the generate function programmatically:
const { generate } = require('@storybook/react-native/scripts/generate');

await generate({
  configPath: './.rnstorybook',
  useJs: false,
  docTools: true,
  host: 'auto',
  port: 7007,
});
This is useful for custom build scripts or tooling.

Watch Mode

The CLI does not have a built-in watch mode. The withStorybook Metro wrapper automatically regenerates the file when your stories change.
For manual watch mode, use a file watcher:
# Using nodemon
nodemon --watch 'components/**/*.stories.*' \
  --exec 'sb-rn-get-stories'

TypeScript Support

The generated .ts file has full TypeScript support:
storybook.requires.ts
interface StoryExports {
  default: any;
  [key: string]: any;
}

type StoriesMap = Record<string, StoryExports>;

const getStories = (): StoriesMap => {
  // Generated story imports
};

export { getStories };

Build docs developers (and LLMs) love