Skip to content

Fix os and tty modules being emptied with Expo API Routes#876

Merged
dannyhw merged 6 commits into
storybookjs:nextfrom
Nezz:fix/ExpoApiRoutes
Apr 23, 2026
Merged

Fix os and tty modules being emptied with Expo API Routes#876
dannyhw merged 6 commits into
storybookjs:nextfrom
Nezz:fix/ExpoApiRoutes

Conversation

@Nezz

@Nezz Nezz commented Apr 11, 2026

Copy link
Copy Markdown
Contributor

Issue: withStorybook replaces os and tty with empty modules on all platforms, breaking Expo API Routes

What I did

withStorybook unconditionally resolved os and tty to { type: "empty" } as a workaround. This breaks Expo API Routes (which bundle for the web platform) when any server-side dependency needs real Node built-ins like os.hostname().

Changes:

  • Enabled path: Scoped the os/tty empty-module override to native platforms only (platform !== 'web'), so web/server bundles get the real Node modules.
  • Disabled path: Removed the os/tty override entirely. When Storybook is disabled, all storybook/@storybook imports are already emptied, so the instrumentor is never loaded and its os/tty imports are never reached.
  • Added tests covering both behaviors.

How to test

  1. Set up an Expo project with API Routes and @storybook/react-native
  2. In an API route, use any library that calls require("os") (e.g. os.hostname())
  3. Verify the API route works without os.hostname is not a function errors
  4. Verify Storybook still loads correctly on iOS/Android (the os/tty override still applies on native platforms)
  • Does this need a new example in examples/expo-example? No
  • Does this need an update to the documentation? No

If your answer is yes to any of these, please make sure to include it in your PR.

@Nezz Nezz requested a review from dannyhw as a code owner April 11, 2026 07:11
@changeset-bot

changeset-bot Bot commented Apr 11, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f0f3f8b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages
Name Type
@storybook/react-native Patch
@storybook/react-native-ui Patch
@storybook/react-native-ui-lite Patch
@storybook/react-native-ui-common Patch
@storybook/react-native-theming Patch
@storybook/addon-ondevice-actions Patch
@storybook/addon-ondevice-backgrounds Patch
@storybook/addon-ondevice-controls Patch
@storybook/addon-ondevice-notes Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

return {
type: 'empty',
};
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intention behind doing this when storybook is disabled wasn't clear to me, but this could be re-added if needed with the web check

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its because I had some reports of these packages getting pulled in somehow even when storybook was disabled

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow, not sure how that could happen 😅

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah i don't know, just metro things 😅

@dannyhw

dannyhw commented Apr 11, 2026

Copy link
Copy Markdown
Member

Makes sense, thanks for the pr 🙏

@dannyhw dannyhw merged commit 95d4211 into storybookjs:next Apr 23, 2026
@Nezz Nezz deleted the fix/ExpoApiRoutes branch April 24, 2026 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants