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 showFix it fast
Most common cause, 1 of 3A dependency ships "use client" and your app is client-only
Silence only this log code with onLog and pass every other log through.
// 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);
},
},
},
});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.
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.
npx vite build 2>&1 | grep -i "directive"Then find the line you see in this list and open its fix:
- 1A dependency ships "use client" and your app is client-only
Every path in the warnings starts with node_modules, and the build still finishes - 2Your own Vite app files still carry "use client" from Next.js code
The paths in the warnings point into src, often in components copied from a Next.js App Router project - 3A component library built for Next.js loses the directive
You 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.
npx vite build 2>&1 | grep -i "directive" | grep -c node_modulesFix
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.
// 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.
npx vite build 2>&1 | grep -ci "directive"Confirmed by rolldown.rs
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.
grep -rln "^['\"]use client['\"]" srcFix
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.
# review each file first, then delete the first-line directive
grep -rln "^['\"]use client['\"]" srcVerify
The build shows no directive warnings for src paths, and the app behaves the same in the browser.
npx vite build && npx vite previewConfirmed by react.dev
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.
head -n 1 dist/*.jsFix
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.
// 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.
head -n 1 dist/*.jsConfirmed by rolldown.rs
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.