Maaz Zindani

Loading the index…

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

Vite and Rollup warn that a "use client" directive was ignored or may not be preserved

Your error

Module level directives cause errors when bundled, "use client" in "src/button.js" was ignored.1 more line it can show

Fix it fast

Most common cause, 1 of 3

A dependency ships "use client" and your app is client-only

Silence only this log code with onLog and pass every other log through.

Fix
// vite.config.js
import { defineConfig } from 'vite';

export default defineConfig({
  build: {
    rolldownOptions: {
      onLog(level, log, handler) {
        if (log.code === 'MODULE_LEVEL_DIRECTIVE') return;
        handler(level, log);
      },
    },
  },
});
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Rollup prints Module level directives cause errors when bundled, "use client" was ignored, and Vite 8 (Rolldown) prints MODULE_LEVEL_DIRECTIVE, because a directive at the top of one module is not kept once modules are merged into a chunk. In a client-only app it is noise: filter MODULE_LEVEL_DIRECTIVE in onLog. For a library aimed at Next.js, restore it with output.banner.

  • Applies to Vite 8.3 (Rolldown 1.2), Rollup 4.68, React 19.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Build once and look only at the directive warnings. The file path in each line tells you which case you are in: paths under node_modules mean cause 1, paths in your own src folder mean cause 2, and a library-mode build whose output must keep the directive is cause 3.

Run this first
npx vite build 2>&1 | grep -i "directive"

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

  1. 1A dependency ships "use client" and your app is client-onlyEvery path in the warnings starts with node_modules, and the build still finishes
  2. 2Your own Vite app files still carry "use client" from Next.js codeThe paths in the warnings point into src, often in components copied from a Next.js App Router project
  3. 3A component library built for Next.js loses the directiveYou build with build.lib, and the published files no longer start with "use client"

What does the error look like?

  • Module level directives cause errors when bundled, "use client" in "src/button.js" was ignored.
  • [MODULE_LEVEL_DIRECTIVE] The semantics of the module level directive "use client" in "src/button.js" may not be preserved when bundling.

Cause 1 of 3

A dependency ships "use client" and your app is client-only

You seeEvery path in the warnings starts with node_modules, and the build still finishes

Check

Component packages add "use client" so they work inside React Server Components frameworks. A Vite single-page app has no server components, so the directive carries no meaning there. Dropping it during bundling changes nothing at runtime, and the warning only adds noise to the build output.

Check
npx vite build 2>&1 | grep -i "directive" | grep -c node_modules

Fix

Silence only this log code with onLog and pass every other log through. In Vite 8 put the hook in build.rolldownOptions (build.rollupOptions is now a deprecated alias); with plain Rollup it goes at the top level of rollup.config.js.

Fix
// vite.config.js
import { defineConfig } from 'vite';

export default defineConfig({
  build: {
    rolldownOptions: {
      onLog(level, log, handler) {
        if (log.code === 'MODULE_LEVEL_DIRECTIVE') return;
        handler(level, log);
      },
    },
  },
});

Verify

Build again. The directive lines are gone, while other warnings, such as large chunk notices, still show.

Verify
npx vite build 2>&1 | grep -ci "directive"

Confirmed by rolldown.rs

Did this fix it?

Cause 2 of 3

Your own Vite app files still carry "use client" from Next.js code

You seeThe paths in the warnings point into src, often in components copied from a Next.js App Router project

Check

React documents 'use client' as a marker for React Server Components, where it draws the line between server-run and client-run modules. Without a server components framework, every module already runs in the browser, so the line in your own files does nothing.

Check
grep -rln "^['\"]use client['\"]" src

Fix

Remove the directive from files in a client-only Vite app. Keep it only in code that is also built by a server components framework such as Next.js.

Fix
# review each file first, then delete the first-line directive
grep -rln "^['\"]use client['\"]" src

Verify

The build shows no directive warnings for src paths, and the app behaves the same in the browser.

Verify
npx vite build && npx vite preview

Confirmed by react.dev

Did this fix it?

Cause 3 of 3

A component library built for Next.js loses the directive

You seeYou build with build.lib, and the published files no longer start with "use client"

Check

Here the directive matters: an app using server components needs it to know the library's components run on the client. Rolldown keeps such a directive only when it sits in an entry module or when output.preserveModules is on. Rollup 4 drops it from bundled files and prints the ignored warning.

Check
head -n 1 dist/*.js

Fix

Put the directive back at the top of every output file with output.banner, which works in Rolldown and Rollup. In Vite 8, preserveModules is a second option that keeps the directive per module.

Fix
// vite.config.js (library mode)
import { defineConfig } from 'vite';

export default defineConfig({
  build: {
    lib: { entry: 'src/index.js', formats: ['es'] },
    rolldownOptions: {
      external: ['react', 'react/jsx-runtime'],
      output: { banner: "'use client';" },
    },
  },
});

Verify

Each built JavaScript file now opens with the directive on its first line.

Verify
head -n 1 dist/*.js

Confirmed by rolldown.rs

Did this fix it?

How do I stop it coming back?

  • Filter by log.code, never by turning off all warnings, so new bundler messages still reach you.
  • In a library repo, add a CI step that fails when a built file is missing the directive on its first line.
  • When moving components from a Next.js project into a Vite app, strip framework-only lines such as the directive in the same commit.

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, QistLock, Taskvare.

Next step

Want this handled for good?

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