Maaz Zindani

Loading the index…

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

Tailwind classes from a shared package are missing in a monorepo app

ErrorTailwind classes work in the app but not in components from a shared package

Short answer

Tailwind generates CSS only for class names it finds while scanning files. In a monorepo, a shared package is often reached through node_modules or sits outside the folder Tailwind starts from, so its classes are never seen. Register that folder with @source, and set the base path with source() if builds start at the repo root.

  • Applies to Tailwind CSS 4, Next.js 15, pnpm workspaces
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Pick a class that only appears in the shared package and search the built CSS for it. If it is absent, the scanner never saw that file.

    npx next build
    grep -rl "your-unique-class" .next/static/css | head
  2. 2Match what you see, then jump to its fix

    1. 1Components from packages/ui have no styling; the same classes work when used directly in the appThe shared package is ignored by automatic detectionFix
    2. 2It works when you run the app from its own folder but fails when a workspace command runs from the repo rootTailwind starts from the wrong base folderFix
    3. 3Only some variants of a component are missing, for example color-based onesClass names are assembled from stringsFix

What does the error look like?

  • Tailwind classes work in the app but not in components from a shared package
  • Styles missing only after next build or only in one app of the monorepo

Cause 1 of 3

The shared package is ignored by automatic detection

1Check

Tailwind scans project files but skips anything in .gitignore and in node_modules. A workspace package linked into node_modules, or a built dist folder, is skipped.

cat .gitignore | grep -E "dist|node_modules"
ls -la node_modules/@acme/ui

2Fix

Tell Tailwind about the folder with @source in the app's stylesheet. The path is relative to that stylesheet, and should point at source files, not at the built output.

/* apps/web/app/globals.css */
@import "tailwindcss";
@source "../../../packages/ui/src";

3Verify

Rebuild and search the CSS output again for the class from the package.

npx next build
grep -rl "your-unique-class" .next/static/css

Confirmed by tailwindcss.com

Did this fix it?

Cause 2 of 3

Tailwind starts from the wrong base folder

1Check

By default the scanner starts from the current working directory. A root-level command makes that the monorepo root, which can pull in the wrong files or miss the ones you expect.

pwd && cat package.json | grep -A5 '"scripts"'

2Fix

Set the base path on the import so scanning is the same wherever the command runs.

@import "tailwindcss" source("../src");
@source "../../../packages/ui/src";

3Verify

Run the build from the repo root and from the app folder; both should produce the same classes.

npx next build

Confirmed by tailwindcss.com

Did this fix it?

Cause 3 of 3

Class names are assembled from strings

1Check

Tailwind reads files as plain text and does not run your code. A name like text-${color}-600 never appears in full, so no CSS is made for it.

grep -rnE "(text|bg|border)-\$\{" packages apps

2Fix

Write complete class names in the source, and choose between them.

// wrong
<div className={`text-${error ? 'red' : 'green'}-600`} />
// right
<div className={error ? 'text-red-600' : 'text-green-600'} />

3Verify

Rebuild and check that both variants have CSS.

npx next build

Confirmed by tailwindcss.com

Did this fix it?

How do I stop it coming back?

  • Keep one @source line per shared package in the app's stylesheet, committed with the app.
  • Point @source at the package's src folder rather than its dist output.
  • Use full class names everywhere; add a lint check for template-built classes.

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

Hydration failed after adding next-themes dark mode

Console reports a server and client mismatch on the html element

next-themes changes the html element and knows the theme only in the browser, so server HTML and client HTML differ.

3 causes, each with a fix

Web apps

Next.js fails with Cannot find module 'tailwindcss'

Error: Cannot find module 'tailwindcss'

Next.js loads Tailwind through PostCSS, so the error means the package named in postcss.config is not installed where the build runs.

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.