Maaz Zindani

Loading the index…

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

Next.js fails with Cannot find module 'tailwindcss'

ErrorError: Cannot find module 'tailwindcss'

Short answer

Next.js loads Tailwind through PostCSS, so the error means the package named in postcss.config is not installed where the build runs. Tailwind v4 needs tailwindcss, @tailwindcss/postcss and postcss, and a config that names the new plugin. On servers, a production install can skip devDependencies.

  • Applies to Next.js 15, Next.js 16, Tailwind CSS 4, Node.js 22, npm 10
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    See which Tailwind packages are installed, and which plugin your PostCSS config asks for.

    npm ls tailwindcss @tailwindcss/postcss postcss
    cat postcss.config.*
  2. 2Match what you see, then jump to its fix

    1. 1npm ls shows (empty) or missing for tailwindcssThe Tailwind packages are not installedFix
    2. 2postcss.config lists tailwindcss: {} as a plugin, with postcss-import or autoprefixerA Tailwind v3 PostCSS config on a v4 installFix
    3. 3Works on your machine, fails in CI, Docker or on the host during next builddevDependencies were not installed on the server or in the imageFix

What does the error look like?

  • Error: Cannot find module 'tailwindcss'

Cause 1 of 3

The Tailwind packages are not installed

1Check

List what npm has installed in this project. All three packages must appear.

npm ls tailwindcss @tailwindcss/postcss postcss

2Fix

Install the three packages for Tailwind v4 and import it from your global CSS.

npm install tailwindcss @tailwindcss/postcss postcss

/* app/globals.css */
@import "tailwindcss";

3Verify

Start the dev server and open a page that uses a utility class.

npm run dev

Confirmed by tailwindcss.com

Did this fix it?

Cause 2 of 3

A Tailwind v3 PostCSS config on a v4 install

1Check

In v4 the PostCSS plugin moved to its own package, and imports and prefixing are handled for you.

cat postcss.config.mjs

2Fix

Use only the new plugin, and replace the three @tailwind directives with one import.

// postcss.config.mjs
const config = {
  plugins: {
    "@tailwindcss/postcss": {},
  },
};
export default config;

3Verify

Restart the dev server after changing the config.

npm run dev

Confirmed by tailwindcss.com

Did this fix it?

Cause 3 of 3

devDependencies were not installed on the server or in the image

1Check

npm skips devDependencies when NODE_ENV is production or --omit=dev is used. If tailwindcss sits in devDependencies, the build step cannot find it.

npm config get omit

2Fix

Install dev dependencies in the stage that runs next build, and only prune them afterwards.

npm ci --include=dev
npm run build

3Verify

Run the same two commands in the failing environment.

npm ls tailwindcss

Confirmed by docs.npmjs.com

Did this fix it?

How do I stop it coming back?

  • Commit package-lock.json and install with npm ci in CI and in Docker.
  • In a multi-stage Dockerfile, run the install and the build in the same stage.
  • After a Tailwind major upgrade, follow the upgrade guide for the PostCSS config and the CSS import.
  • Keep the three Tailwind packages together in package.json.

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 built on the same technologies:

Related fixes

Web apps

Tailwind build says the dark: class does not exist

dark: utilities work in markup but the build fails where @apply uses them in a separate CSS file

The build error means the CSS file being compiled cannot see your dark variant.

3 causes, each with a fix

Next step

Want this handled for good?

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