DEV Community

Set up Storybook with Next.js project and Tailwind

SeongKuk Han on April 08, 2023

Next.js using Tailwind with Storybook Set up a Next.js project with the following command if you have your preferred package manager, you can u...
Collapse
 
ankurnecessary profile image
ankurnecessary • Edited

This solution actually worked. Thanks for sharing this solution. However, I didn't change postcss.config.mjs to postcss.config.js and the versions that I am using in my next.js project are "storybook": "^8.3.2" and "tailwindcss": "^3.4.1". I have only imported global.css in preview.ts.

Collapse
 
oscartrooper profile image
Oscar

Thank you very much for this tutorial, I spent several hours researching how to configure storybook with nextjs and many tutorials on the internet mentioned that I had to modify many files and install more libraries, and in the end none of them worked, your tutorial is the simplest and the only functional one, thanks :)

Collapse
 
lico profile image
SeongKuk Han • Edited

I was there too, that's why I wrote this post. I also installed and uninstalled several packages. I'm really glad it helped :) Have a good day!

Collapse
 
hmdegroot profile image
hmdegroot

Has anyone had luck with passing styles through storybook that aren't compiled in a component already? Like if I pass bg-pink-500 - I'm not using this anywhere in my project so it doesn't work. But if I pass bg-yellow-500 it works since I'm using it elsewhere. Any ideas?

Collapse
 
nithishprem profile image
Prathipati Nithish

Thank you!!
You helped me solve an issue in my react-typescript application built using Vite. The issue I was facing was my tailwind classes are not applied to my storybook components.
The missing piece was the following line in .storybook/preview.ts file,

import "../app/globals.css";
Enter fullscreen mode Exit fullscreen mode

After searching through lot of articles finally I landed on your article and solved the issue. In order for tailwind classes to work we need to import the main CSS file with tailwind directives into preview.ts file in storybook folder.

Once again Thanks!

Collapse
 
marinostbh profile image
Mohamed Amine Terbah

i had an error
node-modules/acron and acron--jsx not found
So i did :
npm i -D @storybook/addon-styling
npm i acorn acorn-jsx
import the global.css file of your tailwind config in .storybook/preview.js => import "../assets/styles/globals.css"
Restart storybook
and it worked

Collapse
 
juanmlglober profile image
juanmlglober

Thanks! U r smarter than chatgpt, copilot, bard and a few friends

Collapse
 
awaisalwaisy profile image
Alwaisy al-waisy

Thank you. really helpful.

Collapse
 
hmdegroot profile image
hmdegroot

Another question, unrelated. This EBUSY error happens essentially anytime I change anything. Is this just the norm? Any good solutions using storybook 7 with NextJS?

Collapse
 
xavierharleyro profile image
Guilherme Xavier

Im using the exact versions that dont work, if I change the storybook version might work ? which is the best version ?

Collapse
 
lico profile image
SeongKuk Han

The new solution I updated doesn't work for you? installing postcss, autoprefixer and creating the postcss configuration file

Collapse
 
kamil_maje55390 profile image
Kamil Majewski

not working with:

"storybook": "^8.0.8",
"tailwindcss": "^3.4.1",

Collapse
 
lico profile image
SeongKuk Han

Thanks for sharing the issue. I have updated the post!

Collapse
 
victorduranem profile image
Victor Duran

This solved it for me. Thank you!

Collapse
 
lico profile image
SeongKuk Han

Glad to hear that it helped you!

Collapse
 
gamercode profile image
Luiz

Thank you