Skip to content

Conversation

@valentinpalkovic
Copy link
Contributor

@valentinpalkovic valentinpalkovic commented Jun 11, 2025

Closes #31549

What I did

Problem

When initializing Storybook in a Next.js v15 project using pnpm, the Storybook UI gets stuck on the loading screen with the following error in the browser console:

Uncaught SyntaxError: The requested module '/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/styled-jsx/index.js?v=1422bc3a' does not provide an export named 'StyleRegistry'

Root Cause: pnpm's Non-Hoisted Dependencies

This is specifically a pnpm issue due to how pnpm handles dependency resolution differently from npm/yarn:

  • pnpm doesn't hoist dependencies - Unlike npm/yarn which hoist dependencies to the root node_modules, pnpm maintains strict dependency isolation
  • styled-jsx is a direct dependency of @storybook/nextjs-vite - In pnpm, it gets installed in a nested path like /node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/styled-jsx/.
    Vite can't resolve the nested dependency - Vite's module resolution expects to find styled-jsx in a more accessible location, but pnpm's strict isolation prevents this. Additionally, Vite has issues to optimize dependencies for transitive dependencies of packages. If styled-jsx is listed in optimizeDeps.include, Vite will not optimize it in pnpm projects if it is a transitive dependency. We have to tell Vite explicitly about the transitive dependency structure.
  • Works fine with npm/yarn - With hoisted dependencies, styled-jsx would be available at the root level where Vite can easily find it

Solution

This fix addresses the pnpm-specific module resolution issue by:

  1. Adding explicit styled-jsx aliases to help Vite resolve the pnpm-nested dependency:
  • Maps styled-jsx to its actual package location using require.resolve()
  • Explicitly resolves styled-jsx/style and styled-jsx/style.js to their correct entry points
  1. Including styled-jsx in Vite's optimization dependencies:
  • Ensures styled-jsx modules are pre-bundled and optimized by Vite
  • Uses the @storybook/nextjs-vite > styled-jsx syntax to reference the nested dependency correctly

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli-storybook/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This pull request has been released as version 0.0.0-pr-31757-sha-dd96f9f4. Try it out in a new sandbox by running npx [email protected] sandbox or in an existing project with npx [email protected] upgrade.

More information
Published version 0.0.0-pr-31757-sha-dd96f9f4
Triggered by @valentinpalkovic
Repository storybookjs/storybook
Branch valentin/nextjs-alias-styled-jsx
Commit dd96f9f4
Datetime Wed Jun 11 21:18:52 UTC 2025 (1749676732)
Workflow run 15595921406

To request a new release of this pull request, mention the @storybookjs/core team.

core team members can create a new canary release here or locally with gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=31757

Greptile Summary

Enhanced Next.js Vite configuration in Storybook to properly handle styled-jsx module aliasing and optimization, fixing loading issues with Next.js v15.3.2.

  • Added resolution aliases for styled-jsx and its style submodules in preset configuration
  • Included styled-jsx in optimizeDeps configuration for better dependency handling
  • Fixed module export errors that were causing Storybook to hang during loading
  • Consolidated path imports using node:path for better maintainability
  • Implemented more reliable path resolution using the resolve utility

@valentinpalkovic valentinpalkovic self-assigned this Jun 11, 2025
@valentinpalkovic valentinpalkovic added ci:normal nextjs bug patch:yes Bugfix & documentation PR that need to be picked to main branch labels Jun 11, 2025
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

LGTM

1 file reviewed, no comments
Edit PR Review Bot Settings | Greptile

@nx-cloud
Copy link

nx-cloud bot commented Jun 11, 2025

View your CI Pipeline Execution ↗ for commit 1a52c00.

Command Status Duration Result
nx run-many -t build --parallel=3 ✅ Succeeded 1m 11s View ↗

☁️ Nx Cloud last updated this comment at 2025-06-12 05:52:48 UTC

@valentinpalkovic valentinpalkovic requested a review from yannbf June 11, 2025 21:15
@valentinpalkovic valentinpalkovic merged commit b3ed76c into next Jun 12, 2025
53 of 57 checks passed
@valentinpalkovic valentinpalkovic deleted the valentin/nextjs-alias-styled-jsx branch June 12, 2025 06:25
@github-actions github-actions bot mentioned this pull request Jun 12, 2025
14 tasks
JReinhold pushed a commit that referenced this pull request Jun 12, 2025
…ed-jsx

Next.js: Enhance Vite configuration with styled-jsx aliasing

(cherry picked from commit b3ed76c)
@github-actions github-actions bot added the patch:done Patch/release PRs already cherry-picked to main/release branch label Jun 12, 2025
@github-actions github-actions bot mentioned this pull request Jun 16, 2025
71 tasks
@ndelangen ndelangen removed the patch:yes Bugfix & documentation PR that need to be picked to main branch label Oct 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug ci:normal nextjs patch:done Patch/release PRs already cherry-picked to main/release branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Storybook initialization hangs on loading screen with Next.js v15.3.2, Vitest, and styled-jsx module export error

3 participants