Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

Prettier formats the file but leaves the Tailwind class order untouched

Your error

npx prettier --write runs without errors, but class lists keep their old order2 more lines it can show

Fix it fast

Most common cause, 1 of 4

The plugin is installed but not listed in the Prettier config

Add the plugin to the config that Prettier reported.

Fix
// .prettierrc
{
  "plugins": ["prettier-plugin-tailwindcss"]
}

// or prettier.config.js
/** @type {import('prettier').Config & import('prettier-plugin-tailwindcss').PluginOptions} */
export default {
  plugins: ['prettier-plugin-tailwindcss'],
};
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

prettier-plugin-tailwindcss sorts nothing when Prettier never loads it: Prettier 3 removed plugin auto-loading, so list it under plugins in .prettierrc, after every other plugin. With Tailwind CSS v4, set tailwindStylesheet to your CSS entry file, and add tailwindFunctions for clsx or cva calls. Applies to Prettier 3.10 and prettier-plugin-tailwindcss 0.8.

  • Applies to Prettier 3.10, prettier-plugin-tailwindcss 0.8, Tailwind CSS 4.3, Next.js 16.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Leave the editor out of it and format one file from the terminal. Print the config file Prettier picks for that file, then check whether the class lists changed. No change from the terminal means the cause is in that config, which every cause below starts from.

Run this first
npx prettier --find-config-path app/page.tsx
npx prettier --write app/page.tsx && git diff --stat app/page.tsx

Then find the line you see in this list and open its fix:

  1. 1The plugin is installed but not listed in the Prettier configThe config file found above has no plugins entry, or one that does not name prettier-plugin-tailwindcss
  2. 2Another Prettier plugin is listed after itThe plugins array names a plugin such as prettier-plugin-organize-imports or @trivago/prettier-plugin-sort-imports after prettier-plugin-tailwindcss
  3. 3Tailwind v4 is used and tailwindStylesheet is not setBuilt-in utilities get sorted, but classes from your own @theme or @utility land at the front of the list
  4. 4The classes live in function calls or non-standard attributesclassName strings are sorted, while strings passed to clsx(), cva() or a cn() helper keep their order

What does the error look like?

  • npx prettier --write runs without errors, but class lists keep their old order
  • Classes inside clsx() or cva() are never reordered
  • Custom theme classes are moved to the start of the class list

Cause 1 of 4

The plugin is installed but not listed in the Prettier config

You seeThe config file found above has no plugins entry, or one that does not name prettier-plugin-tailwindcss

Check

prettier-plugin-tailwindcss does nothing if Prettier never loads it, and Prettier 3 no longer searches for plugins on its own: the 3.0 release removed plugin search and asks for the plugins option or the --plugin flag. Having the package in devDependencies is not enough.

Check
cat "$(npx prettier --find-config-path app/page.tsx)"

Fix

Add the plugin to the config that Prettier reported. In a JavaScript config, the type import gives editor hints for the plugin's options.

Fix
// .prettierrc
{
  "plugins": ["prettier-plugin-tailwindcss"]
}

// or prettier.config.js
/** @type {import('prettier').Config & import('prettier-plugin-tailwindcss').PluginOptions} */
export default {
  plugins: ['prettier-plugin-tailwindcss'],
};

Verify

Run the same write command; git diff now shows reordered class strings in that file.

Verify
npx prettier --write app/page.tsx && git diff app/page.tsx

Confirmed by github.com

Did this fix it?

Cause 2 of 4

Another Prettier plugin is listed after it

You seeThe plugins array names a plugin such as prettier-plugin-organize-imports or @trivago/prettier-plugin-sort-imports after prettier-plugin-tailwindcss

Check

Class sorting can stop when a second Prettier plugin comes later in the list. The Tailwind plugin relies on Prettier APIs that only one plugin can use at a time, and its README lists explicit workarounds for a set of known plugins, all of which depend on the Tailwind plugin being loaded last.

Check
grep -n -A6 "plugins" "$(npx prettier --find-config-path app/page.tsx)"

Fix

Move prettier-plugin-tailwindcss to the end of the array and keep the others in their current order.

Fix
// .prettierrc
{
  "plugins": [
    "@trivago/prettier-plugin-sort-imports",
    "prettier-plugin-tailwindcss"
  ]
}

Verify

Both effects show up in one run: imports are sorted and class lists are reordered.

Verify
npx prettier --write app/page.tsx && git diff app/page.tsx

Confirmed by github.com

Did this fix it?

Cause 3 of 4

Tailwind v4 is used and tailwindStylesheet is not set

You seeBuilt-in utilities get sorted, but classes from your own @theme or @utility land at the front of the list

Check

With Tailwind CSS v4 the sort looks wrong for your own classes when the plugin does not know your CSS entry point: the README says v4 projects must give it through the tailwindStylesheet option. Without your theme the plugin treats those classes as non-Tailwind classes, and Tailwind documents that such classes are always sorted to the front.

Check
grep -rln "@import \"tailwindcss\"" app src --include="*.css" 2>/dev/null

Fix

Point tailwindStylesheet at the file that imports Tailwind. The path is resolved from the folder of the Prettier config, not from the file being formatted. Tailwind v3 projects use tailwindConfig instead, needed only when tailwind.config.js sits somewhere other than next to the Prettier config.

Fix
// .prettierrc
{
  "plugins": ["prettier-plugin-tailwindcss"],
  "tailwindStylesheet": "./app/globals.css"
}

Verify

Format again; a class such as bg-brand from your theme no longer jumps to the front of the list.

Verify
npx prettier --write app/page.tsx && git diff app/page.tsx

Confirmed by github.com

Did this fix it?

Cause 4 of 4

The classes live in function calls or non-standard attributes

You seeclassName strings are sorted, while strings passed to clsx(), cva() or a cn() helper keep their order

Check

The plugin sorts class, className and the framework equivalents, plus @apply, and nothing else by default. Class strings handed to a helper such as clsx or cva, or written in an attribute with another name, stay unsorted until the config names that function or attribute.

Check
grep -rnE "(clsx|cva|cn)\(" app components src 2>/dev/null | head

Fix

List the helper functions in tailwindFunctions and any custom attributes in tailwindAttributes.

Fix
// .prettierrc
{
  "plugins": ["prettier-plugin-tailwindcss"],
  "tailwindStylesheet": "./app/globals.css",
  "tailwindFunctions": ["clsx", "cva", "cn"],
  "tailwindAttributes": ["containerClassName"]
}

Verify

The strings inside those calls are reordered on the next write.

Verify
npx prettier --write "components/**/*.tsx"

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Add npx prettier --check . to CI, so an unloaded plugin shows up as unformatted files instead of passing silently.
  • Pin prettier and prettier-plugin-tailwindcss together in devDependencies; the plugin needs Prettier 3.
  • When a new Prettier plugin is added, keep prettier-plugin-tailwindcss as the last entry in the plugins array.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.