storybook: [Bug]: 'Dynamic require of "@storybook/global" is not supported' error when upgraded to v7.6.3
Describe the bug
When we upgraded all our @storybook/ packages from 7.5.2
to 7.6.3
and then ran a storybook project, we got this error:
'Dynamic require of "@storybook/global" is not supported'
Nothing else has been changed on our side, thus am reporting it as a potential issue from your side.
To Reproduce
Purge package-lock and node_modules and update used sb packages to 7.6.3
then run a storybook in dev or build you should immediately get this error when you open the browser.
System
Storybook Environment Info:
System:
OS: macOS 14.1.2
CPU: (8) x64 Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
Shell: 5.9 - /bin/zsh
Binaries:
Node: 18.12.1 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 8.19.2 - /usr/local/bin/npm <----- active
pnpm: 8.10.2 - /usr/local/bin/pnpm
Browsers:
Chrome: 120.0.6099.62
Safari: 17.1.2
npmGlobalPackages:
@storybook/react: 7.4.5
storybook: 7.5.2
Additional context
we are using react with vite builder for storybook
About this issue
- Original URL
- State: closed
- Created 7 months ago
- Reactions: 4
- Comments: 22 (9 by maintainers)
@shilman I did some investigations, and it seems the webpack
externals
field might not work for CommonJS: https://webpack.js.org/configuration/externals/#externalsThis is a problem.
But it’s very confusing.
Here’s what I’ve been able to gather:
require()
s@storybook/global
. (it does not depend on it in any way at all ⛳ )require()
saddon-actions
(it has a peerDependency on it)addon-actions
, but this is saved by npm installing it, and addon-essentials providing it.@storybook/global
IS in node_modules, tons of other stuff depends on it.De-noising the error:
Now note this:
That’s where the error comes from (note also that webpack does not error in the terminal, but only in the browser) See how there’s a bunch of
webpackEmptyContext
one of them is@storybook/addon-actions/dist
, webpack thinks this is empty!?I’m tracing the breakage futher and I see webpack has outputted this code:
It’s this:
"./node_modules/@storybook/addon-actions/dist sync recursive"
, that seems to very off. It will yield an webpackEmptyContext.That context is later used in addon-console > addon-actions’ bundled code, and it’s not able find it, because …well… its an emptyContext!
The strangest thing is, that there’s even both an externalization for
@storybook/global
AND an webpack alias for it:If we can figure out how and why this “./node_modules/@storybook/addon-actions/dist sync recursive” context is created/is empty we might get closer to a solution.
Out of desperation, I tried a thing: I converted addon-console to ESM … and that made it work.
So that’s 1 path forwards, I guess?
Why that makes it work, I haven’t figured out really.
Pinning
@storybook/addon-actions
to7.5.x
solved the issue for me:@storybook/addon-console
is not a dependency in my project, so there is nothing to upgrade for me. (Not even a transitive dependency, it doesn’t exist in mypackage-lock.json
.)My issue is with stories that use
@storybook/addon-actions
. Should I file a new issue?To follow up, I tried bringing addon-console up to date with modern tooling, and it’s do-able, but looking at the addon, a lot of it doesn’t quite make sense.
typescript
, and also didn’t have atsconfig.json
global.STORYBOOK_ENV
, which should exist, but it’s undefined at runtimeI opened a PR adding ESM, and compiling with tsup
Also running into the same issue trying to upgrade 7.5.3 to 7.6.3. I suspect the root issue is the same and the different error message format stems from vite vs webpack.
Tried explicitly adding
@storybook/global
todevDependencies
inpackage.json
, same issueFor lazy dudes like me coming from Google
7.4.6
works well.Running into similar but different error with same scenario of upgrading from 7.5.2 -> 7.6.3:
Cannot find module '@storybook/global'
after storybook is started up and trying to load a preview. I can reproduce this from 7.6.0 onwards.Interestingly enough didn’t see this in another repo we have that was on 7.0.x -> 7.6.3 so that’s odd.
Edit: using node 20/npm 10, react 18, w/ webpack 5
Hi @soren121, I had the same issue:
Cannot find module '@storybook/global'
when importing@storybook/addon-actions
directly from app code. My circs are pretty niche (web build of react-native app) but I fixed it by aliasing to the ESM build (myresolve.mainFields
are['browser', 'main', 'module']
, defaulting toreact-native
is the correct fix for me but causes other issues)I am getting the same error message when loading up storybook. I was able to pin it down to a 3rd party library that is pretty out of date.
If I remove usage of storybook-react-router then storybook loads as expected.
Looking at storybook-react-router, they have an old dependency on addon-actions.
Running
npm ls @storybook/addon-actions
shows that the version is invalid, but also deduped. So I assume it is just not playing nicely.So to help troubleshoot your own setup, try running
npm ls @storybook/addon-actions
oryarn why @storybook/addon-actions
and see if there are weird/wrong versions.Update: I also had to remove
import '@storybook/addon-console'
from my preview.tsx file. Not sure why yet.We created another fix for this issue (#26145) and released a canary containing it which can be tested by running:
The canary is based on Storybook 8, but once the fix is confirmed and merged, we will patch it to Storybook 7.6 as well.
@stevoland @adamdiestelkamp @DeepRed @soren121 Would you mind testing it out and letting us know? 🙏
Let me chime in and say that I dont use
@storybook/addon-console
but that I am getting the same issue. I am using@storybook/addon-actions": "^7.6.12
Removing the import of addon-actions from an individual story will allow storybook to render correctly. Downgrading to@storybook/addon-actions": "^7.5.3
mitigate the issue and the story renders correctly in storybook. Will go with this for now and watch future releases.Just tried upgrading today from
7.5.3
to7.6.10
and still seeing this issue.Also using Chromatic
10.6.0
@ndelangen Tested on
SB7.6
, works for me!The only thing that seems to work for me is pinning the addons I use to the
7.5.x
range.I use Yarn here.