react-native: Unable to resolve "@storybook/core/manager-api"

Describe the bug I am trying to add storybook on an expo 51 project with expo-router.

To Reproduce Steps to reproduce the behavior:

  1. I have added the withStorybook in metro.config.js
  2. I have added the require().default bit on my index.tsx
  3. When I start without the storybook_enabled envvar, I get this error:
Unable to resolve "@storybook/core/manager-api"

I’ll try to make a repro tomorrow, with 8.3.10.

Expected behavior A clear and concise description of what you expected to happen.

Screenshots Image

Code snippets If applicable, add code samples to help explain your problem.

System: Please paste the results of npx -p @storybook/cli@next sb info here.

Additional context Add any other context about the problem here.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 30 (14 by maintainers)

Most upvoted comments

In Bare React Native 0.76.x, not Expo, it worked fine when I tried the method below.

// more metro config
// ...
  resolver: {
    resolveRequest: (context, moduleName, platform) => {
      if (
        moduleName.startsWith("storybook") ||
        moduleName.startsWith("@storybook")
      ) {
        return context.resolveRequest(
          context,
          moduleName,
          platform,
        );
      } else {
        return MetroSymlinksResolver()(context, moduleName, platform);
      }
    },

My Storybook Version:

    "@storybook/core": "8.4.7",
    "@storybook/addon-actions": "^8.3.4",
    "@storybook/addon-controls": "^8.3.4",
    "@storybook/addon-essentials": "^8.3.4",
    "@storybook/addon-links": "^8.3.4",
    "@storybook/addon-ondevice-backgrounds": "8.4.3",
    "@storybook/addon-ondevice-controls": "8.4.3",
    "@storybook/addon-ondevice-notes": "8.4.3",
    "@storybook/addon-react-native-web": "^0.0.23",
    "@storybook/react": "^8.3.4",
    "@storybook/react-native": "8.4.3",
    "@storybook/react-webpack5": "^8.3.4",

I’m using PNPM Monorepo + RNX-kit metro-resolver-symlinks. If I don’t use metro-resolver-symlinks, I get an error.

My guess is that RNX-kit metro-resolver-symlinks does not support unstable_enablePackageExports properly.

I’ve tried the same with the Expo App, but I get a different error and am analyzing the cause

Encountered similar issue but not using expo. Followed the manual setup doc as well as the multiple entry points for rn

Image

metro.config.js

const withStorybook = require('@storybook/react-native/metro/withStorybook');

const config = {
  resolver: {
    unstable_enablePackageExports: true,
  },
};

const finalConfig = mergeConfig(getDefaultConfig(__dirname), config);

module.exports = withStorybook(finalConfig);```

After some experimenting I got past this with some advice from @dannyhw . I want to add that I was in a situation where I was including the withStorybook conditionally but it didn’t really work right. Also the resolveRequest override upthread didn’t either. Here is what finally did work:

const path = require('node:path');
const { getDefaultConfig } = require('expo/metro-config');
const withStorybook = require('@storybook/react-native/metro/withStorybook');
const { getSentryExpoConfig } = require('@sentry/react-native/metro');
const { withNativeWind } = require('nativewind/metro');

const STORYBOOK_ENABLED =
  process.env.STORYBOOK_ENABLED === 'true' || process.env.STORYBOOK_ENABLED === true;

const sentryCfg = STORYBOOK_ENABLED ? getDefaultConfig(__dirname) : getSentryExpoConfig(__dirname);

const storybookCfg = withStorybook(sentryCfg, {
  enabled: STORYBOOK_ENABLED,
  onDisabledRemoveStorybook: !STORYBOOK_ENABLED,
  configPath: path.resolve(__dirname, './.storybook-mobile'),
});

const nativewindCfg = withNativeWind(storybookCfg, {
  input: './global.css',
  hotServerOptions: { port: 8089 },
});

module.exports = nativewindCfg;

I am including the other config-decorators in here so you can see how I handled this in the context of others.

this is the critical line that ‘just worked’ for storybook:

const storybookCfg = withStorybook(sentryCfg, {
  enabled: STORYBOOK_ENABLED,
  onDisabledRemoveStorybook: !STORYBOOK_ENABLED,
  configPath: path.resolve(__dirname, './.storybook-mobile'),
});

trying to conditionally include the withStorybook was less successful than just using the flags.

You need to create a metro config file its required in the setup (see readme).

npx expo customize metro.config.js

See getting started

https://github.com/storybookjs/react-native#getting-started

@hanwenbo please double check your metro is configured correctly, this is an issue with package exports not being enabled when storybook runs.

module.exports = isSbEnabled ? withStorybook(config) : defaultConfig

@OmkarK45 you can do that but thats kinda the whole point of the enabled option, though you can check the source code to see how it works too

https://github.com/storybookjs/react-native/blob/next/packages/react-native/src/metro/withStorybook.ts

Not a lot of code here so you can also just apply the same settings manually if you want.

if enabled = false we just return the config as it was.

You need to create a metro config file its required in the setup (see readme).

npx expo customize metro.config.js

See getting started

https://github.com/storybookjs/react-native#getting-started

thanks @dannyhw , i got it working perfectly