storybook: [Bug]: Storybook doesn't run with tailwindcss in next.js
Describe the bug
Describe the bug
- I have a default next.js setup
- To apply tailwind to Storybook, I followed the official docs
- Tailwindcss has applied to my next.js app when I run
yarn devbut not to the stories when I runyarn storybook. - I did some experiments and these results came out:
-
Storybook picks up tailwindcss in these cases.
- There is only
postcss.config.jsin the project(root directory) - There are both
postcss.config.mjsandpostcss.config.jsin the project(root directory)
- There is only
-
Storybook doesn’t pick up tailwindcss in this case.
- There is only
postcss.config.mjsin the project
- There is only
To Reproduce
You can clone this repo and run yarn storybook
Make sure to remove postcss.cofnig.mjs to see the failure.
https://github.com/algoORgoal/bug-storybook-tailwindcss
System
Storybook Environment Info:
System:
OS: Windows 11 10.0.22631
CPU: (8) x64 Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
Binaries:
Node: 20.12.2 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.22 - ~\AppData\Roaming\npm\yarn.CMD <----- active
npm: 10.5.2 - ~\AppData\Roaming\npm\npm.CMD
Browsers:
Chrome: 123.0.6312.123
Edge: Chromium (123.0.2420.97)
npmPackages:
@storybook/addon-coverage: ^1.0.1 => 1.0.1
@storybook/addon-essentials: ^8.0.8 => 8.0.8
@storybook/addon-interactions: ^8.0.8 => 8.0.8
@storybook/addon-links: ^8.0.8 => 8.0.8
@storybook/addon-onboarding: ^8.0.8 => 8.0.8
@storybook/blocks: ^8.0.8 => 8.0.8
@storybook/nextjs: ^8.0.8 => 8.0.8
.17.0
eslint-plugin-storybook: ^0.8.0 => 0.8.0
msw-storybook-addon: ^2.0.0-beta.2 => 2.0.0-beta.2
storybook: ^8.0.8 => 8.0.8
Additional context
“postcss”: “8.4.38”, “storybook”: “8.0.8”, “tailwindcss”: “3.4.1”, “next”: “14.2.1”,
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 8
- Comments: 15 (2 by maintainers)
Commits related to this issue
- fix: setup tailwind for storybook https://github.com/storybookjs/storybook/issues/26869 — committed to ZZIPSA/kkuk-kkuk-fe by 2chanhaeng 3 years ago
Thank you! I just spent several hours trying to get tailwind to work in storybook. This did the trick.
postcss.config.cjsexport default config;tomodule.exports = config;tsconfig.jsonto include"**/*.cjs"in theincludearray.I think this framework needs an update ASAP to make this change, as the default install is completely broken without it.
@storybook/nextjs 8.0.9. next 14.2.2. tailwindcss 3.4.3. postcss 8.4.38.I have the same problem.
A workaround to fix it is change the extensions of files
postcss.configandtailwind.configto.cjs. Must be include too a**/*.cjsat yourtsconfig.jsonas this example.~This is working, but I don’t know why.~
Why it works?
Under the hood,
@storybook/nextjsuses postcss-loaderhttps://github.com/storybookjs/storybook/blob/67a21117d31991758175790f37fc4ea1cc5ea22f/code/frameworks/nextjs/src/css/webpack.ts#L35
The file
postcss.config.mjsis not a valid config file for this loaderIn addition to doing the following:
postcss.config.cjsexport default config;tomodule.exports = config;tsconfig.jsonto include"**/*.cjs"in the include array.You need to change
preview.jsto import theglobals.cssfile (or whatever file has yourtailwindcssimports:import "../src/app/globals.css";If you are just finding this issue, do just this and it will work. A couple of hours wasted looking around until I found this lol
Thank you very much for the solution @drianoaz
Note for others - I only had to change the postcss config to
postcss.config.cjsto get things working, and was able to leave the tailwind config astailwind.config.tsWow, thank you so much! I think it would have been better if it was documented somewhere. Maintainers, please let us know if pull requests are welcomed 😃
Thanks @nady90, it’s working now.
The issue was that I had
important: '#__next',in mytailwind.config.tsfile and that caused the issue.@valentinpalkovic I have just updated
storybookand all the addons tov8.1.3and can confirm that for me it is now picking up and using the esmpostcss.config.jsfile (as well as still supporting the older.cjsversion). 👍https://github.com/storybookjs/storybook/pull/26707 should have fixed this issue. Can someone please upgrade to Storybook v8.1 and see if the issue is fixed?
https://github.com/storybookjs/storybook/issues/26869#issuecomment-2086052901
Thank you @nady90, this was driving me crazy, your workaround works for me with Storybook 8.0.9 and Next 14.2.3 🎉