[JIT] Tailwind does not update classes after saving in NextJS
What version of Tailwind CSS are you using?
2.1
What build tool (or framework if it abstracts the build tool) are you using?
"next": "10.1.3",
What version of Node.js are you using?
v14.15.1
What browser are you using?
chrome
What operating system are you using?
macOS for M1
Reproduction repository
https://github.com/mwarger/next-tailwind-jit-repro
Describe your issue
I've also put reproduction instructions in the repro repo.
It seems that changes made while in jit mode are not being picked up. As it stands now, with jit mode enabled, only initial styles are applied, and kept. Any changes or additions result in loss of styles. Please advise. Thanks!
See #4073
Same issue.
Creating a .env file and setting TAILWIND_MODE=watch fixed this for me. I'm not sure why this should be required, but I can at least move forward for now...
I can confirm having this bug in NextJS intermittently with no way to tell if it's going to occur or not. (Apparently unrelated to #4073).
It does occur (without any visible error) if I include a Tailwind plugin that is missing or PostCSS configuration file that is invalid. But it also occurs for no apparent reason, and with no obvious output that's different.
JIT not working -- no JIT message, webpack warning:

JIT working -- JIT message, no webpack warning:

These are the same project with npm run dev (next start).
Windows 10 x64, tailwindcss 2.1.1, next 10.1.3 using webpack 5 option (I updated tailwind to 2.1.2@latest yesterday to monitor the issue further, it could have been fixed) (I'm also going to update to next 10.2 today)
The error will come & go while working in Next.js -- seems to be working fine, then suddenly JIT fails without any visible output. Restarting the next dev server will generally produce the right styles one time but is no guarantee JIT will start working.
Here's the list of everything I tried, but nothing seemed to make a difference:
- Remove all Tailwind plugins from
tailwind.config.js - Add
variants: ["responsive"]totailwind.config.js - Turn it off & turn it back on (this one works! lol)
- Create
.envfile withTAILWIND_MODE=watchper @mwarger - Swap
npm run devfromnext devtoenv TAILWIND_MODE=watch NODE_ENV=development next dev
I think @Danon is on the right track with there being some type of a race condition (see #4073). Nice job with that research! 👍
Environment: node v15.14.0 windows x64 chrome latest stable
In my case, the problem is temporarily solved by changing the file tailwind.config.js after starting the project.

Interesting, thanks for the tip!
For me, it also works normally with webpack 4, i.e. turning off the webpack 5 option in next.config.js
On Sun, May 2, 2021, 10:38 AM Ivan Reshetnikov @.***> wrote:
In my case, the problem is temporarily solved by changing the file tailwind.config.js after starting the project. [image: tailwind-trigger] https://user-images.githubusercontent.com/17311456/116818714-67724f00-ab75-11eb-8e63-595c3c2845a3.png
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/tailwindlabs/tailwindcss/issues/4081#issuecomment-830828257, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFZ3D2VONYMFNNGQXJOFYTTLVWXXANCNFSM427P472A .
Dropping in to also comment how JIT will intermittently stop working when using NextJS. Just started a new project today, Tailwind with JIT was working flawlessly, then all of a sudden my new css classes weren't being applied. Restarting the dev server and restarting VSCode did not fix the problem either
EDIT: I can also confirm that changing text in tailwind.config.js also temporarily fixes the problem
Same here! 🤚 (Next 10.2 with Webpack 5) Seems only occur with Webpack 5 for me.
Same problem here (Next 10.1.3 and Next 10.2). The problem occurs with both Webpack 4 and 5 for me and none of the above suggestions like the watch mode environment variables have helped.
Had to do this to make JIT work:
TAILWIND_MODE=watch webpack serve
Maybe watch mode should be enabled if NODE_ENV !== 'production'? It's common to not specify NODE_ENV=development
Reverting to webpack 4 improves but doesn't fix the reliability issue for me. I haven't tried the edit config file when it stops working suggestion.
Guillaume, could you elaborate? You're using webpack serve instead of next dev?
I was having the same issue of JIT not updating the CSS classes with barebone webpack 5.
I've also had this happen to me seemingly randomly in my Nextjs project (Webpack5 and I believe Webpack 4 as well). Saving changes just won't update CSS. For instance, I applied w-32 and h-32, and started changing those values and was real confused why it wasn't working. I disabled JIT, everything started working, then re-enabled JIT and it was fine.
I had the same issue, yesterday and today.
And my way to fix it was:
- Stop dev server
- Comment out mode: 'jit' line in tailwind.config.js
- Start dev server
- Stop dev server
- Uncomment mode: 'jit'
✨ And it works ✨ Follow me for more "Restart tutorials" 🤣 jk
Just cloned down the reproduction — to get it working I needed to add TAILWIND_MODE=watch to the start script. Internally Tailwind will do this automatically if NODE_ENV=development so I guess for whatever reason with this setup NODE_ENV isn't set by default.
Going to close as resolved but if anyone else is having issues feel free to open new issues with your own reproduction. Eventually we will make all this TAILWIND_MODE stuff less clunky — unfortunately really no easy way to detect if the user is running a watch process or just doing a one-off build though so I suspect it will always require some kind of configuration if we can't depend on NODE_ENV.
Maybe it would be a NODE_ENV !== 'production' so that it works if NODE_ENV isn't set?
And then it can print out a warning message like
Tailwind JIT is in watch mode because NODE_ENV environment variable isn't set.
In a lot of development environments NODE_ENV is not explicitly set for development, and set to production for production building.
Thanks for looking into this!
Here's what I'm using now for Webpack 5 with Next.js on Windows 10:
package.json
"dev": "env TAILWIND_MODE=watch NODE_ENV=development next dev",
or
env TAILWIND_MODE=watch NODE_ENV=development npx next dev
npm run dev

I still have this problem once or twice a day.
But my config is mega simple, I have nothing changed, it is out-of-the-box with nextjs.
But yeah, from time to time I just have to comment the line mode: 'jit' and reset dev server. It is strange.