How to Fix: Experimental: useLightningcss doesn’t actually work

5 min read

Enabling useLightningcss in Next.js can break builds because the flag depends on a very specific CSS processing path that many real projects do not follow cleanly.

If your app crashes or behaves inconsistently after uncommenting useLightningcss in next.config.js, the problem is usually not your CSS syntax alone. The failure typically comes from how Next.js experimental CSS compilation, PostCSS plugins, and your dependency graph interact when Lightning CSS replaces part of the default transformation pipeline.

Understanding the Root Cause

The useLightningcss option is marked experimental for a reason: it swaps CSS handling to a different engine with different expectations and feature support. In projects like the one linked in the issue, the build may fail after enabling it because Lightning CSS is not a drop-in replacement for every PostCSS-based workflow.

Here is what is happening technically:

  • Next.js normally processes CSS through its established pipeline, which often includes PostCSS, framework presets, and plugin-based transforms.
  • When useLightningcss is enabled, Next.js attempts to delegate more of the CSS transformation and optimization work to Lightning CSS.
  • If your project depends on PostCSS plugins, nonstandard syntax, CSS module assumptions, or tooling that expects the classic pipeline, the experimental path can break.
  • Some builds fail only in development, others in production, because the CSS compilation path is not always identical across modes.

In practical terms, this issue usually appears when one or more of the following are true:

  • Your project uses a custom PostCSS configuration that Lightning CSS does not fully replace.
  • A dependency ships CSS that relies on behavior tolerated by the default pipeline but rejected or transformed differently by Lightning CSS.
  • Your installed Next.js version exposes the flag before the feature is stable enough for your exact setup.
  • The repository mixes global CSS, CSS modules, and package CSS imports in a way that the experimental compiler path mishandles.

The key point is this: the bug is not always that Lightning CSS is entirely broken. It is that the experimental implementation does not yet reliably support all combinations of Next.js CSS loading, third-party styles, and PostCSS-dependent transforms.

Step-by-Step Solution

The safest fix is to stop treating useLightningcss as production-ready and either remove it or isolate the project so that the Lightning CSS path has nothing conflicting to process.

1. Revert the experimental flag

If your goal is a stable build, remove or disable the option in next.config.js.

/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    // useLightningcss: true,
  },
}

module.exports = nextConfig

This is the most reliable solution if you need the application working immediately.

2. Upgrade Next.js before testing again

If you want to keep testing the feature, first move to the latest compatible version of Next.js, because experimental CSS issues are often fixed in canary or newer stable releases.

npm install next@latest react@latest react-dom@latest

Or with Yarn:

yarn add next@latest react@latest react-dom@latest

Then retry with the flag enabled.

3. Check for a custom PostCSS setup

If the repository includes a postcss.config.js file, simplify it temporarily. For example, if you have plugin chains that are not strictly necessary, remove them and test again.

module.exports = {
  plugins: {
    autoprefixer: {},
  },
}

If the build works after simplification, the issue is likely a PostCSS plugin compatibility conflict.

4. Test without third-party CSS imports

Package-level CSS can trigger failures if a dependency ships syntax or constructs that the experimental compiler path does not handle the same way.

Temporarily comment out imported styles in your app entry or layout files and re-run the build.

import '../styles/globals.css'
// import 'some-package/styles.css'

If the issue disappears, inspect third-party CSS imports first.

5. Clear the build cache

Next.js can hold onto previous artifacts. After toggling CSS compilation behavior, remove the cached output.

rm -rf .next
npm run dev

On Windows PowerShell:

Remove-Item -Recurse -Force .next
npm run dev

6. Validate whether CSS features are supported by Lightning CSS

If your styles use advanced nesting, custom media handling, or plugin-driven transforms, move those rules into a minimal test case. A simple reduction helps prove whether the syntax itself is the trigger.

/* test.module.css */
.container {
  display: flex;
  gap: 1rem;
}

If simple CSS works but your real stylesheet fails, the problem is likely feature support mismatch rather than project configuration.

7. Use the stable workaround

For most teams, the correct answer today is:

  • Keep useLightningcss disabled
  • Use the default Next.js CSS pipeline
  • Only re-enable the flag after confirming your Next.js version and CSS stack support it
/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {},
}

module.exports = nextConfig

This avoids spending time debugging a compiler path that is explicitly experimental.

Common Edge Cases

  • Tailwind or plugin-heavy PostCSS setups: Even if the app builds without Lightning CSS, enabling it can expose assumptions in your CSS tooling chain.
  • Dependency CSS from npm packages: External stylesheets may contain syntax that compiles differently or fails only under the experimental engine.
  • Dev works, production fails: Development and production optimizations are not always identical, so test both next dev and next build.
  • App Router and Pages Router differences: Mixed routing structures can make it harder to identify where CSS is imported and which part triggers the crash.
  • Stale lockfiles: If your dependency graph changed across test runs, remove node_modules and reinstall to ensure consistent results.
rm -rf node_modules package-lock.json .next
npm install
npm run build

FAQ

Is this a bug in my project or in Next.js?

Usually both factors are involved, but the issue strongly points to an experimental Next.js feature limitation. If disabling useLightningcss fixes the app without other code changes, the root problem is almost certainly in the experimental CSS path.

Can I safely use useLightningcss in production?

Not unless you have fully tested your build pipeline, dependencies, and deployment flow. Because the flag is experimental, the safer recommendation is to leave it off for production workloads.

What is the fastest way to confirm the cause?

Disable the flag, delete .next, and run the app again. If the error disappears immediately, then Lightning CSS compatibility is the trigger. After that, test with a reduced PostCSS config and fewer CSS imports to identify the exact conflict.

The practical resolution for this GitHub issue is straightforward: do not rely on useLightningcss unless your project has been verified against the experimental compiler path. In the current state, the stable Next.js CSS pipeline is the correct default and the most dependable fix.

Leave a Reply

Your email address will not be published. Required fields are marked *