Skip to content

Next.js-vite: Fix module resolution in preset with fileURLToPath #32386

Merged
shilman merged 1 commit into
nextfrom
norbert/esm-nextjs-vite-resolve-fix
Sep 3, 2025
Merged

Next.js-vite: Fix module resolution in preset with fileURLToPath #32386
shilman merged 1 commit into
nextfrom
norbert/esm-nextjs-vite-resolve-fix

Conversation

@ndelangen
Copy link
Copy Markdown
Member

@ndelangen ndelangen commented Sep 3, 2025

Closes #32378

What I did

The reference to the previewAnnotation was not wrapped with fileURLToPath which is needed for vite in some situations

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

I have manually verified this fix solves the problem by copying over the dist into a failing reproduction.
Afterwards the sandbox worked.

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-32386-sha-09a230ff. Try it out in a new sandbox by running npx storybook@0.0.0-pr-32386-sha-09a230ff sandbox or in an existing project with npx storybook@0.0.0-pr-32386-sha-09a230ff upgrade.

More information
Published version 0.0.0-pr-32386-sha-09a230ff
Triggered by @shilman
Repository storybookjs/storybook
Branch norbert/esm-nextjs-vite-resolve-fix
Commit 09a230ff
Datetime Wed Sep 3 14:08:29 UTC 2025 (1756908509)
Workflow run 17436120265

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=32386

Greptile Summary

Updated On: 2025-09-03 13:02:04 UTC

This PR fixes a critical ESM module resolution bug in the Next.js Vite framework preset by wrapping three import.meta.resolve() calls with fileURLToPath(). The issue was causing Storybook to fail on fresh installs with Next.js Vite, producing malformed file paths that mixed absolute filesystem paths with file:// URL protocols.

The problem occurs because import.meta.resolve() returns file URLs (starting with file://), but Vite's module resolution system expects standard filesystem paths. Without proper conversion, the resulting paths were malformed, causing import resolution failures during development.

The fix applies fileURLToPath() to three key module references:

  • The builder-vite name resolution (line 28)
  • The React renderer preset (line 33)
  • The Next.js Vite preview annotations (line 38)

This change aligns with Node.js ESM best practices and maintains consistency with other parts of the same file that were already correctly using fileURLToPath() for styled-jsx module resolution. The fix is targeted and minimal, addressing only the specific paths that were causing the resolution failures while preserving all existing functionality.

Confidence score: 5/5

  • This PR is safe to merge with minimal risk as it fixes a clear bug without changing functionality
  • Score reflects a straightforward bug fix following established patterns already present in the codebase
  • No files require special attention beyond the single modified preset file

@ndelangen ndelangen changed the title Refactor: Use fileURLToPath for module resolution in Next.js Vite preset NextjsVite: Use fileURLToPath for module resolution in preset Sep 3, 2025
@ndelangen ndelangen self-assigned this Sep 3, 2025
@ndelangen ndelangen added bug nextjs ci:normal Run our default set of CI jobs (choose this for most PRs). labels Sep 3, 2025
@nx-cloud
Copy link
Copy Markdown

nx-cloud Bot commented Sep 3, 2025

View your CI Pipeline Execution ↗ for commit 09a230f

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

☁️ Nx Cloud last updated this comment at 2025-09-03 13:00:20 UTC

@ndelangen ndelangen marked this pull request as ready for review September 3, 2025 13:00
Copy link
Copy Markdown
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.

1 file reviewed, no comments

Edit Code Review Bot Settings | Greptile

@shilman shilman changed the title NextjsVite: Use fileURLToPath for module resolution in preset Next.js-vite: Use fileURLToPath for module resolution in preset Sep 3, 2025
Copy link
Copy Markdown
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

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

Thanks @ndelangen . Tested and it fixes the problem. @JReinhold can you please do a technical review? I'm merging & releasing to unblock our announcement.

@shilman shilman merged commit 98649af into next Sep 3, 2025
63 of 67 checks passed
@shilman shilman deleted the norbert/esm-nextjs-vite-resolve-fix branch September 3, 2025 14:19
@github-actions github-actions Bot mentioned this pull request Sep 3, 2025
8 tasks
@shilman shilman changed the title Next.js-vite: Use fileURLToPath for module resolution in preset Next.js-vite: Fix module resolution in preset with fileURLToPath Sep 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug ci:normal Run our default set of CI jobs (choose this for most PRs). nextjs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[SB10-beta]: Next.js-vite broken on fresh install

2 participants