Dynamic Imports Break Stories in Storybook v9 (Next.js 15) #32130
Replies: 1 comment
-
Hi @shaquille-galanto! I'm Dosu and I’m helping the storybook team. This issue happens because Vite can't analyze dynamic imports like next/dynamic at build time, which leads to runtime errors in Storybook—especially with Next.js 15 and @storybook/nextjs-vite. This is a known limitation and is being tracked in the Storybook repo. Using static imports works because Vite can resolve them correctly, but dynamic imports break with errors like the one you posted. See details here. There isn't a built-in fix in Storybook or @storybook/nextjs-vite for all next/dynamic use cases. The recommended workarounds are: use static imports in your stories when possible, or mock next/dynamic with a custom implementation or alias in your Vite/Storybook config. Some users have had success by adjusting module aliasing and adding the storybookNextJsPlugin to their vitest.config.ts, which can help with module resolution but doesn't guarantee a fix for all dynamic import issues. See discussion and example. If you need a more tailored workaround, could you share a code snippet of how you're using next/dynamic? That might help narrow down a project-specific solution. To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Hi, I'm using Storybook with Next.js 15 and running into an issue when using
next/dynamic
. When I try to render components that use dynamic imports, I get the following error in Storybook:However, when I replace the dynamic import with a static one, the story renders correctly.I also tried mocking the next/dynamic module in my Storybook config, but no luck getting it to work.
I've included my Storybook configuration details and version info below. Would appreciate any guidance on how to properly handle next/dynamic in storybook. Thanks in advance!
Additional information
Storybook configuration
Storybook version
Error Message
Create a reproduction
No response
Beta Was this translation helpful? Give feedback.
All reactions