storybook: Uncaught TypeError: Cannot read property 'fonts' of undefined
Describe the bug
Ran yarn storybook after installing as per docs. And in browser console this error showed up

System:
System:
OS: macOS 11.0
CPU: (8) x64 Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz
Binaries:
Node: 13.11.0 - /usr/local/bin/node
Yarn: 1.22.4 - /usr/local/bin/yarn
npm: 6.13.7 - /usr/local/bin/npm
Browsers:
Chrome: 84.0.4147.125
Firefox: 79.0
Safari: 14.0
npmPackages:
@storybook/addon-actions: ^6.0.6 => 6.0.6
@storybook/addon-essentials: ^6.0.6 => 6.0.6
@storybook/addon-links: ^6.0.6 => 6.0.6
@storybook/node-logger: ^6.0.6 => 6.0.6
@storybook/preset-create-react-app: ^3.1.4 => 3.1.4
@storybook/react: ^6.0.6 => 6.0.6
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 34
- Comments: 56 (16 by maintainers)
Commits related to this issue
- Fix mismatched emotion versions This caused some weird runtime errors in Storybook https://github.com/storybookjs/storybook/issues/12016 — committed to InVisionApp/dsm-storybook-example-library by baiirun 4 years ago
- [LIST-460] Run of yarn-deduplicate. See https://github.com/storybookjs/storybook/issues/12016 — committed to australiangreens/ag-internal-components by anthonyblond a year ago
- fix(docs): Upgrade storybook to 7.6.9 to fix e.typography error (#2514) https://github.com/storybookjs/storybook/issues/12016 — committed to swisspost/design-system by imagoiq 5 months ago
I have had the same problem.
When you remove node_modules directory and then run
yarn installornpm install, this errors will disappear.But in my case, after that I had another errors:
I think that the problem is with integration between emotion and storybook after the upgrade by
sb upgradescript. The above errors are from level of emotion code.My package.json:
For me, solution was removing /node_modules and yarn.lock (and then installing everything again). I know that it is not professional solution, but it works.
This issue has come back for us now:
Error: Cannot read properties of undefined (reading 'fonts'). We’ve had it before, maybe a little less than a year ago, but I can’t exactly remember what triggered it then. Updating the packages fixed the issue I think (back then).What happened now; I moved some code using
<IconGallery />from an.mdx-file to a.tsx-file and this triggered the error, just like @vamshi9666 describes 3 years ago. So it works in MDX, but not in tsx. We just upgraded to7.4.6last week. I have deleted node modules, doesn’t help. I’d like to avoid deletingpackage-lock.json. We only use tailwind, so seems like it’s Storybooks own packages conflicting?Screenshots of the mdx component that works, and the tsx component that fails, not sure how much value these adds though… I can’t easily create a reproduction unfortunately.
From console:
I just had the aforementioned problem with the latest version of Storybook (7.5.2). Indeed no
@emotionoccurrences. However, installing@storybook/themingworked for me.This is an issue with multiple
@emotion/coredependencies across the tree, which can be helped by forcing the version across all packages. Beware this might break any package using the older versions. In my case I was using@chakra-ui/coredepending on@emotion/core@10.0.35while storybook was depending on@emotion/core@10.0.28, so (I’m hoping) not too big of a change.To force the versions, either do
yarn install --flatand choose the version of the package you want to use, or add aresolutionsfield topackage.jsonwith the package name and the required version, after which you should delete the lock-file and reinstall.For more information, see https://classic.yarnpkg.com/en/docs/cli/install/#toc-yarn-install-flat
Following @Nvveen tip, I found yarn-deduplicate and running
yarn-deduplicate --packages @emotion/core,@emotion/styledactually worked! It removed the duplicates on myyarn.lockand it seems to be working just fine 🙂Storybook’s doc blocks require a theme context to work. I think if you provide one in a decorator you should be fine. This is not really supported usage, however.
I tried to remove the
node_modulesand reinstall it, and it can runs.Egads!! I just released https://github.com/storybookjs/storybook/releases/tag/v6.1.0-alpha.28 containing PR #12874 that references this issue. Upgrade today to the
@nextNPM tag to try it out!Closing this issue. Please re-open if you think there’s still more to do.
Same here guys =0
I’m running into this as well. I upgraded our storybook from 5.3 to 6 and started seeing this. I suspect that it has to do with conflicting versions of emotion, I know i’ve seen that happen before. I have version 10.0.22 of @emotion/core installed, 10.0.19 of emotion-theming and a decorator that looks like this:
@vamshi9666 I have seen this as well when trying to use
IconGalleryandIconIteminside astories.js(seems to be fine inside.mdx) story. Was this your experience as well?@sarahbethfederman are you using ColorPalette/ColorItem in your stories or on your docs pages?
@ayeletcohen Glad you figured out a workaround. Hopefully we can come up with something better as part fo the 6.1 release.
Whoopee!! I just released https://github.com/storybookjs/storybook/releases/tag/v6.0.27 containing PR #12874 that references this issue. Upgrade today to the
@latestNPM tag to try it out!@anilanar Storybook’s manager (surrounding UI code) uses one version of emotion. Its preview (user code) doesn’t use emotion at all, except if you’re using
addon-docsand in the docs tab. So if you want to use a different version of emotion, don’t useaddon-docs(you can disable it from essentials), and that might be all you need. If that doesn’t work, perhaps there is some yarn or webpack resolution magic that forces the right version … but I haven’t tried that yet.For the npm team, using
npm dedupefixed it for me docs.npmjs.com/cli/dedupeLooks great, but I think we need to keep find out a solution that would be “dependencies manager agnostic”. So we need to handle this dependency issue without specific npm or yarn fix.
Deleting yarn.lock isn’t an option. I deleted node_modules however the issue still persists!!
I’ve isolated the issue a bit further - I think there’s a bug where things simply break if there are multiple instances of
@storybook/themingin your dependency tree.@tylercraft try using this in your
webpackFinal!@fastndead, yes, ultimately what counts is what you have in your package-lock.json. So look it up there to be 100%.
I also get this error message again. The strange thing is, that I can’t find any occurrence of “@emotion” in the whole project, including node_modules…
@sarahbethfederman it shouldn’t be a problem. unsupported just means that we won’t go out of our way to keep it working between minor version bumps and won’t provide migration instructions on major bumps. i’m hoping we’ll rework a bunch of this stuff later this year, and perhaps then we can better support your use case. 🤞
For anybody who’s experiencing this problem, what happens when you run Storybook with the
--no-dllflag?For anyone trying to get this to work with Gatsby, @nscmnto and @Nvveen’s solution works. 😃
Thanks! In my case, running
yarn-deduplicate, pointing@emotion/coreto10.0.28inpackage.jsonresolutions, removingnode_modulesand thenyarnseemed to work 😃