Sub topic 4
Add the plugin to vite.config
This chapter assumes you have a working Vite project and have already installed tailwindcss and @tailwindcss/vite, as in the previous chapters. You should be comfortable reading ES module import syntax and know that Vite reads a config file at startup. Here you register the plugin so that Vite can process Tailwind in your CSS.
What the plugin does
Vite is built around a plugin pipeline. Every file the browser requests, including CSS, passes through a chain of plugins that can transform it. Tailwind v4 ships a first-party plugin, @tailwindcss/vite, that hooks into this chain. It does three jobs:
- It finds CSS files that contain Tailwind directives such as
@import "tailwindcss". - It scans your project for class names and generates only the CSS those classes need.
- It tells Vite to update the page when a class change produces new CSS, so hot reload works.
Without the plugin, Vite treats @import "tailwindcss" as a plain CSS import. It either fails to resolve the import or serves the raw Tailwind source with none of your utilities generated. The plugin is what turns that line into real styles.
In v3 you needed PostCSS, autoprefixer, a postcss.config.js, and a tailwind.config.js. With the Vite plugin, a typical project needs none of those. The only required change is one entry in vite.config.
Find your config file
Vite looks for a config file in the project root. The accepted names are vite.config.js, vite.config.mjs, vite.config.ts, vite.config.cjs, vite.config.mts, and vite.config.cts. The npm create vite@latest templates generate vite.config.js for JavaScript templates and vite.config.ts for TypeScript templates. Edit the file that exists. Do not create a second one.
If no config file exists, create one. A vanilla JavaScript template sometimes has none, because Vite works without one.
Register the plugin
Open the config file and make it look like this:
import { defineConfig } from 'vite'
import tailwindcss from '@tailwindcss/vite'
export default defineConfig({
plugins: [
tailwindcss(),
],
})There are three parts to understand.
import tailwindcss from '@tailwindcss/vite'loads the plugin factory. The nametailwindcssis your choice, but using the package name avoids confusion with the coretailwindcsspackage, which you never import in this file.tailwindcss()calls the factory. It must be called. Writingplugins: [tailwindcss]passes the function itself and Vite will not apply it as a plugin.pluginsis an array. Vite runs plugins in order, with some exceptions that depend on each plugin'senforcesetting.
The same file in plain JavaScript is identical. Only the extension changes:
import { defineConfig } from 'vite'
import tailwindcss from '@tailwindcss/vite'
export default defineConfig({
plugins: [
tailwindcss(),
],
})defineConfig is optional at runtime. It exists so your editor can offer type hints and autocomplete. Keep it, because it costs nothing and catches typos in option names.
Combine it with a framework plugin
Most real projects already have a framework plugin in the array. Add Tailwind next to it. For a React project created from the Vite template, the file looks like this:
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
export default defineConfig({
plugins: [
react(),
tailwindcss(),
],
})The two plugins do not depend on each other. The React plugin handles JSX and Fast Refresh. The Tailwind plugin handles CSS. The order between them rarely matters, and the common convention is to list the framework plugin first.
The same pattern works for Vue (@vitejs/plugin-vue), Svelte (@sveltejs/vite-plugin-svelte), and Solid. In each case, add tailwindcss() to the existing array and leave the other entries alone.
Do not add a second CSS pipeline
Do not also add the PostCSS plugin @tailwindcss/postcss for the same project. If both run, Tailwind processes your CSS twice, which can produce duplicated output or confusing errors. Choose one integration. The PostCSS route is covered in a later chapter.
Module format and Node version
The plugin package is an ES module. This matters in two situations.
vite.config.cjsor a CommonJS config. A config written withrequire()can fail to load an ESM-only package. The error usually mentionsERR_REQUIRE_ESMor says the module cannot be loaded withrequire. Rename the file tovite.config.mjsorvite.config.tsand useimportsyntax."type": "module"inpackage.json. The Vite templates set this already. With it,.jsfiles are treated as ES modules, sovite.config.jscan useimportdirectly.
Vite compiles vite.config.ts itself, so you do not need ts-node or any extra TypeScript setup for the config file.
Tailwind v4 and current Vite versions require a supported Node release. If npm install or npm run dev fails with syntax errors from inside node_modules, run node --version and compare it with the Node requirement listed in the Vite and Tailwind docs. Upgrading Node fixes that class of error.
Restart the dev server
Vite reads vite.config once, when the dev server starts. Vite usually detects changes to the config file and restarts the server by itself, but do not rely on it. After you edit the file, stop the server with Ctrl+C and start it again:
npm run devIf you registered the plugin while the server was running and styles still do not appear, a stale server is the first thing to rule out.
Check that the plugin loaded
At this point you have the plugin registered, but nothing is imported yet. The CSS import comes in the next chapter. You can still confirm the config is valid:
- Run
npm run dev. The server should start without errors. A config problem, such as a wrong import path or a syntax error, appears in the terminal before the server prints its local URL. - Run
npm ls @tailwindcss/vite tailwindcss. Both packages should be listed at the same major version, 4.x.
If npm ls shows tailwindcss at 3.x, an older install is in the way. Remove it with npm uninstall tailwindcss, then run npm install tailwindcss @tailwindcss/vite again.
Common mistakes in this step
| Symptom | Likely cause | Fix |
|---|---|---|
| `Cannot find package '@tailwindcss/vite'` | The package is not installed, or it was installed in a different folder | Run `npm install tailwindcss @tailwindcss/vite` in the folder that holds `vite.config` |
| Server starts, but classes do nothing | The plugin is imported but not added to `plugins`, or `tailwindcss` is not called | Add `tailwindcss()` inside the array, with parentheses |
| `ERR_REQUIRE_ESM` when starting Vite | The config is CommonJS | Rename to `vite.config.mjs` or `.ts` and use `import` |
| Styles are duplicated or the build errors in CSS processing | Both `@tailwindcss/vite` and `@tailwindcss/postcss` are active | Remove the PostCSS plugin from `postcss.config` |
| Edits to `vite.config` seem ignored | The dev server is still running with the old config | Stop and restart `npm run dev` |
A leftover tailwind.config.js is not read
If you are migrating from v3, you may still have tailwind.config.js and a postcss.config.js that lists tailwindcss and autoprefixer. In v4 the Vite plugin does not load tailwind.config.js automatically. Configuration moves into CSS, which a later chapter covers with @theme. A leftover v3 PostCSS config can still run, because Vite loads postcss.config.* on its own. Remove the tailwindcss entry from it, or delete the file if nothing else uses it.
Why this design
Using a Vite plugin instead of a PostCSS plugin has a practical benefit. The Vite plugin sees Vite's module graph, which is Vite's record of which files import which. That lets Tailwind rebuild only when a relevant file changes and keeps dev-server updates fast. The PostCSS plugin works one level lower and sees only CSS, so it has less information. For that reason Tailwind recommends the Vite plugin when your project uses Vite, and the PostCSS plugin is the fallback for setups that cannot use it.
Exercise
A teammate adds import tailwindcss from '@tailwindcss/vite' to vite.config.ts and writes plugins: [react(), tailwindcss]. The dev server starts, but no utility classes work. What is wrong, and how do you fix it?
Show answer
The plugin factory is passed without being called. Vite needs the plugin object that tailwindcss() returns, not the function itself. Change the array to plugins: [react(), tailwindcss()] and restart the dev server.
Exercise
Your project has a vite.config.cjs that uses require('@tailwindcss/vite'), and Vite fails with ERR_REQUIRE_ESM. Name two changes that fix this.
Show answer
Rename the file to vite.config.mjs (or vite.config.ts) and change require to import. Alternatively, set "type": "module" in package.json, use a vite.config.js file, and write it with import syntax.