Sub topic 14
Troubleshooting common problems
This chapter assumes you have a Vite project with Node.js and npm, that you have installed tailwindcss and @tailwindcss/vite, and that you know the setup steps from earlier chapters. Here you will learn to find the cause of the most common failures. Most of them come from four places: the plugin, the CSS import, the stylesheet load, and class detection.
Work through the pipeline in order
Tailwind only works when every link in the chain is in place. Check the links in the order a class travels through them.
Problems and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| Page is unstyled, no errors | The CSS file is never imported | Add `import './style.css'` to your entry file |
| Page is unstyled, CSS file is loaded | Missing `@import "tailwindcss";` | Add the import at the top of the CSS file |
| Raw `@tailwind base;` shows up, or build errors on it | v3 directives left in the CSS | Replace them with `@import "tailwindcss";` |
| Error that a `tailwind.config.js` is ignored | v4 does not read it automatically | Move tokens into `@theme` |
| Some classes work, others do not | Class names built dynamically | Write complete class names |
| Classes from a library do nothing | The library sits in `node_modules` | Add an `@source` line |
| Module not found for `@tailwindcss/vite` | Package not installed | Run `npm install tailwindcss @tailwindcss/vite` |
The plugin is installed but not registered
Installing the package does nothing until Vite loads it. Open vite.config.ts and confirm both the import and the call.
import { defineConfig } from 'vite'
import tailwindcss from '@tailwindcss/vite'
export default defineConfig({
plugins: [
tailwindcss(),
],
})A common slip is writing tailwindcss without the parentheses. The plugin is a function, so it must be called.
Dynamic class names are not detected
Tailwind scans your files as plain text and looks for complete class names. It does not run your code. A string assembled at runtime is invisible to the scanner.
const color = 'blue'
// Not detected: "bg-blue-600" never appears as a whole string
const bad = `bg-${color}-600`
// Detected: both full names appear in the source
const good = color === 'blue' ? 'bg-blue-600' : 'bg-red-600'Files outside automatic detection
Tailwind skips files that .gitignore ignores, and it skips node_modules. If a component library there uses Tailwind classes, point Tailwind at it.
@import "tailwindcss";
@source "../node_modules/@my-company/ui-lib";The path is relative to the CSS file that contains the @source line.
Stale results after a change
If you changed vite.config.ts or installed a package, restart the dev server. Vite does not reload its config plugins on its own in every case. If the problem remains, stop the server, delete node_modules/.vite, and start again.
rm -rf node_modules/.vite
npm run devSetups that cannot use the Vite plugin
If your toolchain forces PostCSS, use @tailwindcss/postcss instead. Do not register both the Vite plugin and the PostCSS plugin, because that processes the CSS twice.
export default {
plugins: {
"@tailwindcss/postcss": {},
},
}Exercise
Your page shows no Tailwind styling. The vite.config.ts file registers tailwindcss(), and style.css contains @import "tailwindcss";. The browser's network tab shows no request for the CSS. What is the most likely cause, and how do you fix it?
Show answer
The app never imports the stylesheet. Add import './style.css' to the entry JavaScript or TypeScript file (or link it from the HTML), then reload the dev server.
Exercise
A button uses className={bg-${tone}-500} and has no background. Why, and what is one fix?
Show answer
Tailwind scans for complete class names, and bg-blue-500 never appears as a whole string in the source. Write the full names, for example a lookup object such as { blue: 'bg-blue-500', red: 'bg-red-500' }, so each class appears literally.