That NEXT_PUBLIC_ variable ships to the browser
NEXT_PUBLIC_* values are inlined into the client bundle. Anything private stays on the server.
Published here , updated · 3 sources · Resume reading
Next.js inlines NEXT_PUBLIC names into browser JavaScript at build time, so anyone who downloads the bundle can read those values [1]. Names without that prefix stay on the server. After a build, grep the static output for the prefix and for any secret you never meant to ship.
Quick start: name the secret without the public prefix
Put client-safe config under a public prefix. Put anything that can open a database, charge a card, or mint a session under a name with no public prefix.
cat > /tmp/env-demo/.env <<'EOF'
NEXT_PUBLIC_ANALYTICS_ID=G-PUBLICONLY
DATABASE_URL=postgres://app:[email protected]:5432/app
STRIPE_SECRET_KEY=sk_test_do_not_ship
EOFIn app code, read the public id from process.env.NEXT_PUBLIC_ANALYTICS_ID in client components. Read DATABASE_URL and STRIPE_SECRET_KEY only in Route Handlers, Server Components, or other Node-only paths [1]. The Next.js docs say a regular environment variable is only available in the Node.js environment, and that Next.js can inline a value at build time into the JS bundle delivered to the client when the name uses the NEXT_PUBLIC_ prefix [1].
Prove what landed in the client bundle
You do not need a full Next app to understand the rule. The build replaces references to public names with hard-coded strings. After a real next build, the static folder under .next/static is the place to look.
Simulated client chunk (the same shape a bundler leaves behind):
mkdir -p /tmp/env-demo/.next/static/chunks
printf '%s\n' 'self.__NEXT_DATA__={};const x="G-PUBLICONLY";/* NEXT_PUBLIC_ANALYTICS_ID inlined */' \
> /tmp/env-demo/.next/static/chunks/main-app.js
cd /tmp/env-demo
grep -r 'NEXT_PUBLIC_' .next/static
grep -r 'G-PUBLICONLY' .next/static
grep -rE 'sk_test|DATABASE_URL|postgres://' .next/static || echo 'no secrets in client output'Captured output:
.next/static/chunks/main-app.js:self.__NEXT_DATA__={};const x="G-PUBLICONLY";/* NEXT_PUBLIC_ANALYTICS_ID inlined */
.next/static/chunks/main-app.js:self.__NEXT_DATA__={};const x="G-PUBLICONLY";/* NEXT_PUBLIC_ANALYTICS_ID inlined */
no secrets in client outputThe public analytics id is searchable in the static tree. The database URL and Stripe secret are not. On a real project, run the same greps after next build. If a private string appears under .next/static, the name that held it was public (or something else copied the value into client code). Move the secret to a server-only name, remove the client reference, rebuild, and grep again [1].
Public-prefix values become hard-coded strings in the client bundle; server-only names never appear under .next/static.
Same trap in Vite and Expo
Vite exposes variables prefixed with VITE_ on import.meta.env after bundling. Its docs warn that VITE_* values are bundled into source at build time and should not hold API keys [2]. Expo replaces process.env.EXPO_PUBLIC_* references when you run Expo CLI, and says not to store private keys in those names because they show up in plain text in the compiled app [3].
# Vite
VITE_SOME_KEY=123 # client
DB_PASSWORD=foobar # not exposed
# Expo
EXPO_PUBLIC_API_URL=https://staging.example.com # client
EXPO_PUBLIC_API_KEY=abc123 # also client: treat as publicIf you migrate between frameworks, rename the prefix and re-check the built assets. A secret that was safe as DATABASE_URL in Next becomes a leak the moment someone renames it to NEXT_PUBLIC_DATABASE_URL "so the client can connect."
What belongs on the client
Safe client values are ones you would paste into a public README without rotating keys: a marketing site URL, a publishable analytics measurement id, a Stripe publishable key (pk_...), a feature-flag client key that only toggles UI. Unsafe client values are database URLs, Stripe secret or restricted keys (sk_..., rk_...), private API tokens, session signing secrets, and service-account JSON.
Publishable Stripe keys are designed for browsers. Secret and restricted keys are not. Putting STRIPE_SECRET_KEY behind NEXT_PUBLIC_ is the classic mistake this page exists to stop [1].
Changing a public value requires a rebuild because the old string is already baked into the deployed JS [1]. Hosting env panels that only restart the Node process will update server-only names; they will not rewrite an already shipped client chunk until you build again.
Mistakes that ship secrets
Prefixing a database URL or private API token with NEXT_PUBLIC_ so a client component can "just read it," then discovering the string under .next/static after release.
Reading process.env.SECRET in a Client Component and assuming the bundler will keep it server-side. Client modules that touch process.env for a public name get inlining; a non-public name often becomes undefined in the browser, which tempts people to flip the prefix instead of moving the call to a Route Handler [1].
Committing .env because create-next-app put it in .gitignore "so it should be fine," then copying the same file into a Docker build context that ignores .dockerignore rules. The public values are meant to be public; the rest still need a secrets store.
Treating Expo EXPO_PUBLIC_API_KEY as private because the name says key. Expo documents that those values are visible in the compiled application [3].
Skipping the post-build grep after a rename. The UI can look fine while the previous secret still sits in an old chunk on a CDN until cache expiry.
How this fits agent and webhook work
Agents that edit .env will happily add the public prefix when a client error says a variable is undefined. Keep a short allowlist of public names in the repo README, and reject new NEXT_PUBLIC_ entries that look like secrets in review. For Claude Code projects that sit next to real env files, lock Claude Code out of your .env so the model cannot paste private values into client files in the first place.
Webhooks and payment confirmations belong on the server with secret keys. Client code should call your own route, not hold the Stripe secret that six Stripe webhook checks assume stays private.
CI check you can paste
Add a job step after the production build that fails if a denylist of secret shapes appears under the static output. Keep it boring:
# after next build
if grep -RInE 'sk_live_|sk_test_|postgres://|BEGIN (RSA |OPENSSH )?PRIVATE KEY' .next/static; then
echo 'secret-shaped string found in client bundle' >&2
exit 1
fi
grep -RIn 'NEXT_PUBLIC_' .next/static | headThe first grep is the gate. The second is a review aid so you still see which public names shipped. Expand the denylist for your stack (AWS key prefixes, GitHub PATs, Slack bot tokens). False positives on docs strings are rare in .next/static; when one appears, narrow the pattern instead of deleting the check.
Preview deployments deserve the same grep. A "temporary" NEXT_PUBLIC_STRIPE_SECRET on a Vercel preview is still a public URL with a downloaded bundle.
Dynamic lookup does not save you
Expo documents that only static process.env.EXPO_PUBLIC_* property access is inlined; bracket access is not [3]. Next's public inlining is also a build-time rewrite of references it can see [1]. People sometimes write process.env[name] hoping to hide a secret from the bundler while still reading it on the client. That usually yields undefined in the browser, which sends them back to adding the public prefix. Move the call to a Route Handler or server action instead of inventing a cleverer client read.
Feature-flag vendors and analytics SDKs often want a client key. That is fine when the vendor designed the key to be public. Copying the same pattern onto your own admin API token is how incidents start.
When the grep is clean
After next build (or your Vite/Expo production build), the static tree shows only the public ids you intended, and a search for sk_, postgres://, and private token prefixes returns nothing. Deploy that build. Rotate any secret that ever appeared under a public prefix, because browsers and scrapers may already have it.
Server-only names keep working when you change them in the host's env panel and restart. Public names need a fresh build. That asymmetry is the whole design: the browser bundle is a snapshot, not a live view of process.env [1][2][3].
Before any public AI paste of a work PDF, run the file-class check in check approval before uploading work documents.
Browse the Dev tools topic.
Send this to the friend who put a private API key behind a NEXT_PUBLIC name.

Keep this guide
Questions about this guide
How do I find keys shipped to the browser?
After building, grep the static output for the public prefix and for secret substrings you never meant to ship, for example grep -r 'NEXT_PUBLIC_' .next/static and a second pass for sk_ or postgres://.
Do other frameworks have this?
Yes. Vite exposes VITE_* on the client. Expo inlines EXPO_PUBLIC_*. Always check whether your framework's prefix ships to the client.
Does changing a NEXT_PUBLIC_ value need a rebuild?
Yes. Next.js inlines the value at build time into the JS bundle, so a new value only appears after you rebuild and redeploy that bundle.
Sources
- Next.js docs: environment variablesnextjs.org
- Expo docs: environment variablesdocs.expo.dev