Why your build drops 'use client' from the bundle

Your build can drop 'use client' from the bundle while the line is still sitting right there in your source file. That is the whole bug. It took me a long time to even name it, because the first thing you do is open the file — and the file is fine.
The problem lives one layer down. Your bundler is allowed to merge several source files into one output file, and when it does, only one of them gets to keep its first line.
I hit this twice while building nextjs-logger, a package that lets you log from anywhere in a Next.js app. Both times the symptom was the same: it worked in next dev, broke in next build, and said nothing. No error in the terminal. No error in the browser. The build just shipped the wrong thing.
The quick answer, if you just hit this
Does your 'use client' component (or your 'use server' action) work in next dev and break in next build? Then open the built file, not the source file. Your bundler probably merged your file into a bigger output chunk, and that chunk does not start with the directive.
A directive is not a label attached to your function. It is a plain string that has to be the very first line of the built file. If something else lands ahead of it, it stops counting.
That is the short answer. The rest of this post is the longer one, because “go read the compiled output” is easy to say and annoying to actually do.
What a directive really is, at the bundler level
When you write:
'use client'
export function Counter() {
// ...
}You probably think of 'use client' as an annotation on the Counter function, similar to a decorator or a doc comment. It isn’t. It’s a string literal at the top of the file, and the only thing that reads it is the bundler’s static analysis pass, at build time, looking at the first statement of each emitted file.
That distinction matters because of what a bundler like esbuild (which powers tsup, which is what I use to build this package) does by default: it merges multiple source modules into a single output chunk whenever it decides that’s more efficient — shared dependencies, small files, anything imported once from one place. That merge is invisible to you unless you go looking at dist/. Your source tree still shows the directive exactly where you put it. The compiled file esbuild actually wrote might not.
So the rule I eventually wrote down for myself, and now treat as load-bearing:
A directive survives in the output only if the module carrying it becomes its own build entry point, emitted as its own file, with nothing else merged ahead of it.
If esbuild folds your directive-bearing module into a shared chunk together with some other file, whichever file’s content happens to come first in that chunk determines the prologue — and if that’s not your directive-bearing module, the directive is just gone. Not commented out, not moved. Absent. No warning, because from the bundler’s point of view nothing went wrong: it did exactly what bundlers are supposed to do, which is optimize output size by merging modules.
Bug #1: 'use client' silently dropped from a re-exported component
The first time this hit me, nextjs-logger had a client bootstrap component that got imported like this:
// src/provider/LoggerProvider.tsx — an async Server Component
import { LoggerBootstrapClient } from './client'And provider/client.ts was the barrel carrying the directive:
// src/provider/client.ts
'use client'
export { LoggerBootstrapClient } from './LoggerBootstrapClient'This looked completely correct, and it was correct — as TypeScript, as React, as source code you’d read in a review. The bug was that './client' wasn’t in the external list for that particular tsup build entry. Without that, esbuild saw one import used from exactly one place and decided the efficient thing to do was hoist LoggerBootstrapClient straight into the chunk that imports it, and rewrite provider/index.js to pull the component from that shared chunk instead of from its own file.
The result: dist/provider/client.js still existed on disk. The 'use client' directive was still sitting right there at the top of it. It just wasn’t being imported by anything anymore — dead code, still shipped, still technically present, completely irrelevant. The real LoggerBootstrapClient that the rest of the app actually imported now lived inside a merged chunk that had no directive at its head, because the chunk’s prologue was determined by whatever else got merged into it first.
So the component silently became a Server Component. (That client half is the piece that makes browser logs reach your terminal without a public endpoint work at all.) Not an error. Just… a component that used to run in the browser, now running on the server, doing nothing useful there, and no client-side logging ever fired. If you’ve debugged a “why does this only work in dev” issue before, you know how quiet that failure mode is — everything typechecks, everything lints, the dev server (which doesn’t do this kind of aggressive chunk merging) works fine, and the bug only exists in the specific artifact you’re about to ship.
The fix was one line: add './client' to the external array for that build entry, forcing esbuild to treat it as a hard module boundary instead of something eligible for merging. But finding that line required actually diffing dist/provider/index.js against dist/provider/client.js and noticing the import had been rewritten to point somewhere else.
Bug #2: 'use server' needs the function body in the directive file — not just a re-export
The second bug looked almost identical on the surface but had a different root cause, and it’s the more dangerous one because it fails in a way that looks like it’s working.
If you put 'use server' on a file that only re-exports a Server Action defined somewhere else:
// barrel.ts — WRONG
'use server'
export { relayLogEntries } from './server-action'The directive is technically present in your source. Next’s build tooling still won’t register the action correctly, because Next needs the function body itself to be in the file carrying the directive — that’s what it instruments and what it writes into server-reference-manifest.json, the file that maps an action’s ID back to the actual function Next should call when a client invokes it. A directive on a barrel that just re-exports doesn’t get you that. The manifest entry either doesn’t get created, or points at nothing usable, and calling the action from client code silently no-ops instead of throwing.
For this package specifically, that meant the fallback relay path — the Server Action nextjs-logger falls back to when the primary route handler 404s — would appear to succeed (no thrown error, the await resolves) but never actually deliver anything server-side. A logging library that silently drops logs on its own fallback path is about as bad as bugs get for a logging library (see also: why your serverless function loses its last logs, a different cause with the same shape), so this is the one I now have an explicit regression test for: tsup.config.ts builds that Server Action entry with splitting: false, meaning the whole module — directive and function body together — has to land in one unsplit output file, no exceptions, no merging.
'use client' is different: it only needs a boundary, not the whole subtree
Here’s the part that made this genuinely confusing while I was debugging it, because the two directives don’t behave the same way and my instinct kept applying the wrong lesson from one to the other.
'use server' needs the actual function body present, because Next has to instrument that specific function.
'use client', by contrast, only needs to mark a boundary — a directive-bearing barrel file is enough, even if it re-exports something defined elsewhere, because everything reachable from that boundary is understood to be client code regardless of which file physically contains it. That’s why src/provider/client.ts in this package can safely stay inside a normal code-split build: as long as some file in the chain that Next’s compiler walks carries the directive at the top of its emitted output, the boundary holds.
So the two failure modes are asymmetric, and “it re-exports the thing, so the directive should apply” is right for one directive and wrong for the other. I don’t think that’s obvious from reading the React docs — I only learned it by watching both bugs fail in different, specific ways.
How I actually made this un-regressable
Catching this by re-reading source code doesn’t work, because the source code is correct. The only reliable check is comparing the compiled output against known-bad shapes, so I wrote scripts/check-build-contract.mjs, which runs in postbuild (meaning a broken artifact literally cannot get published — the build step itself fails) and does two things:
- Opens every
dist/file that’s supposed to carry a directive and asserts the directive is the literal first statement. - Traces the actual import graph from
dist/provider/index.jsto confirm it resolves to the file that has the directive, not to some other copy that got hoisted into a shared chunk.
And because a checker script can rot into something that always passes without you noticing, there’s a test — src/__tests__/build-contract.test.ts — that takes a working dist/ output, mutates a copy of it to reintroduce each of these two historical bugs one at a time, and asserts the checker actually rejects the mutated version. If someone (probably future me) “simplifies” the checker in a way that stops catching bug #1 or bug #2, that test fails immediately, in CI, before anything ships.
The takeaway if you’re building your own package with directives
If you ship a package with 'use client' or 'use server' boundaries and you bundle with esbuild, tsup, or Rollup, don’t trust that your source tree reflects your shipped behavior. Go look at what’s actually in dist/ — specifically, check whether the module carrying your directive got merged into a bigger chunk, and if it did, check whether that chunk’s first statement is still the directive or something else that got merged in ahead of it.
And if you can, write the test the mutates-a-known-good-build-and-checks-the-checker-still-catches-it way, not just the “does the current build pass” way. A checker that only ever runs against passing builds can’t tell you it still works — it can only tell you it hasn’t failed yet.