storybook: Storybook throw error if used emotion 11
Describe the bug Problem with emotion 11 : Module not found: Error: Can’t resolve ‘@emotion/styled/base’
To Reproduce
yarn storybook
Code snippets
import styled from '@emotion/styled'
export const Text = styled.span`
color: #000;
`
System
OS: macOS 11.0.1
CPU: (4) x64 Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz
Node: 14.15.1 - /var/folders/0l/rm1hc02j23nb4nd9lzpwxf1m0000gp/T/fnm_multishell_78935_1606337172547/bin/node
Yarn: 1.22.4 - /usr/local/bin/yarn
npm: 6.14.8 - /var/folders/0l/rm1hc02j23nb4nd9lzpwxf1m0000gp/T/fnm_multishell_78935_1606337172547/bin/npm
Chrome: 87.0.4280.67
Firefox: 83.0
Safari: 14.0.1
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 85
- Comments: 32 (6 by maintainers)
Commits related to this issue
- Typo in addon-controls documentation There is a number() typo instead of text() — committed to storybookjs/storybook by pascaloliv 4 years ago
- Fix emotion 11 configuration https://github.com/storybookjs/storybook/issues/13277 — committed to openscript/react-section-dividers by openscript 4 years ago
- Enable docs Some additional aliasing in `.storybook/main.js` resolved the rendering errors we saw previously. For more context, see: https://github.com/storybookjs/storybook/issues/13277#issuecommen... — committed to CatchRelease/arbor by pvenable 3 years ago
- Enable docs Some additional aliasing in `.storybook/main.js` resolved the rendering errors we saw previously. For more context, see: https://github.com/storybookjs/storybook/issues/13277#issuecommen... — committed to CatchRelease/arbor by pvenable 3 years ago
- fix(storybook): add support for Emotion v11 in the current React + Storybook setup https://github.com/storybookjs/storybook/issues/13277 — committed to nrwl/nx by juristr 3 years ago
Why is this closed? this is still an issue affecting storybook and emotion 11
By adding
"@emotion/core": toPath("node_modules/@emotion/react"),it starts to work:I also needed to change the definition of the actions addon. Please see: https://github.com/openscript/react-section-dividers/blob/0f22f4c483ed5f1abf226a73001bd91276797281/.storybook/main.js
Thank you @mverissimo and @mavlikwowa for looking into this. 🎄
It didn’t work for me. I’m using yarn workspaces, so
node_modulesis in some parent directory. I also have the bad tendency of moving files around, so I wanted a solution that keeps working.This is what I came up with:
Just replace
toPath('node_modules/@emotion/react')withgetPackageDir('@emotion/react')and you’re good to go.In my opinion this issue should be reopened. I’m really stuck with this. I had to downgrade to
emotion10.You need to use babel-preset-css-prop to development and
@emotion/babel-pluginonly for production.I changed webpack.config in Storybook and it helped! `const path = require(“path”)
const toPath = (_path) => path.join(process.cwd(), _path)
module.exports = { stories: [‘…/stories/**/*.stories.@(ts|tsx|js|jsx)’], addons: [‘@storybook/addon-links’, ‘@storybook/addon-essentials’], // https://storybook.js.org/docs/react/configure/typescript#mainjs-configuration typescript: { check: true, // type-check stories during Storybook build }, webpackFinal: async (config) => { return { …config, resolve: { …config.resolve, alias: { …config.resolve.alias, “@emotion/core”: toPath(“node_modules/@emotion/react”), // You should add this row “@emotion/styled”: toPath(“node_modules/@emotion/styled”), “emotion-theming”: toPath(“node_modules/@emotion/react”), }, }, } }, }; `
Even with @emotion removed from plugins and I add “@emotion/babel-preset-css-prop” to presets I still getting
Module not found: Error: Can't resolve '@emotion/styled/base'Their documents suggests it pulls in the plugin anyways.
What I did:
yarn.lockandnode_modulesyarn installyarn start:storybookI’ve pushed the changes to the repo and branch mentioned above.
@openscript after updating my
webpackFinalin themain.jsto what you have above, I am still getting the “cannot redefine property: ClassNames” error:Here is my
main.js:If you want this fixed, please upvote by adding a 👍 to the issue description. We use this to help prioritize!
@HosseinAgha i didn’t close this issue. if you want to track the open issue for emotion 11, it is https://github.com/storybookjs/storybook/issues/13145. we are actively working on a proper fix, though it’s non-trivial because we are also under the constraints of semver and are not ready to do a major release.
@HosseinAgha I didn’t close the issue. I just referenced it from the Nx repo where we implemented a fix for this.
Hey @shilman, the workarounds provided here are all hacky and not a fix.
The Webpack alias solution does not work for a yarn (v2) pnp environment.
@juristr could you please reopen the issue? I don’t think any of the provided solutions resolve the core issue.
I still think this is a serious issue with either storybook or emotion 11.
I had the problem (emotion 11) but i have only replaced @emotion/core by @emotion/react in my devDependencies and it’s ok now ! I hope it will help others!
@samvv MY HERO! 🔥
@openscript thanks for the fix! I can confirm that the fix works for me. 👍
Note: my project uses Gatsby, TypeScript, Emotion (I use styled and the css prop), and Storybook. I found this post particularly helpful in resolving issues I was running into using the emotion css prop. https://duncanleung.com/emotion-css-prop-jsx-pragma-storybook/
Here are the steps I ran through to resolve.
./storybook/webpack.config.jsyarn.lockandnode_modules/.cacheyarn storybookHere is my
main.jsAnd here is my
webpack.config.jsAfter I removed
"emotion-theming": toPath("node_modules/@emotion/react"),it seems to work better, but other runtime errors occur.