Next.js reads up to four files from the project root and merges them into process.env. When the same name appears in several places, the first one in this list wins:
- Variables already set in the real environment, for example by your shell, Docker or the hosting platform.
.env.$(NODE_ENV).local, such as.env.development.local..env.local, skipped whenNODE_ENVistestso tests behave the same on every machine..env.$(NODE_ENV), such as.env.production..env, the shared defaults.
NODE_ENV is set for you: next dev uses development, while next build and next start use production. The convention that follows is simple. Commit .env and .env.development only if they hold safe defaults, keep personal and secret values in .env.local, and git-ignore every .local file. Values can also reference each other with $NAME, and a literal dollar sign is written \$.
# Server-only: never reaches the browser
DATABASE_URL=postgres://app:app@localhost:5432/store
STRIPE_SECRET_KEY=sk_test_51Hx0000000000000000
# Public: inlined into the client bundle at build time
NEXT_PUBLIC_SITE_URL=http://localhost:3000
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_51Hx0000000000000000Without the prefix, a variable exists only in Node.js: Server Components, Route Handlers, Server Actions and API routes can read it, and the browser never sees it. With the NEXT_PUBLIC_ prefix, next build searches your client code for process.env.NEXT_PUBLIC_SOMETHING and replaces it with the literal value. The bundle that ships to visitors contains the string itself.
"use client";
// Inlined at build time: the bundle contains the literal "pk_test_..."
const publishableKey = process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY;
// NOT inlined: dynamic lookups and destructuring stay undefined in the browser
const name = "NEXT_PUBLIC_SITE_URL";
const broken = process.env[name];
const { NEXT_PUBLIC_SITE_URL } = process.env;Two consequences catch teams out. First, the value is frozen into the build: change NEXT_PUBLIC_API_URL on your hosting platform and nothing happens until the next build. Second, everything with the prefix is public. A secret key with NEXT_PUBLIC_ in front of it is published to every visitor, and renaming it afterwards doesn't remove it from bundles already cached or deployed.
Rotate anything that was ever public
If a secret was given the NEXT_PUBLIC_ prefix and deployed, treat it as leaked: rotate it at the provider, then move it to a server-only name. Publishable keys meant for browsers, such as a Stripe publishable key or a public analytics ID, are fine with the prefix.
Server code reads process.env when it runs, but a page that is statically rendered runs during next build, so the value it sees is the one present at build time. To read the value of the environment the app is running in, render the code per request. In recent Next.js versions, awaiting connection() from next/server does that explicitly; reading cookies or headers has the same effect.
import { connection } from "next/server";
export default async function StatusPage() {
await connection(); // render per request, not at build time
const region = process.env.DEPLOY_REGION ?? "unknown";
return <p>Served from {region}</p>;
}This is the basis of the build once, deploy many pattern: one Docker image for staging and production, with server-side values injected when the container starts. It works for server-only variables. Client code still needs either a build per environment or a small server endpoint or Server Component that passes the runtime value down as a prop.
To keep secrets from leaking into client code by accident, put the modules that read them behind import "server-only", which makes the build fail if a Client Component imports them. Scripts and test setups that run outside Next.js can load the same files with loadEnvConfig from @next/env. The Next.js environment variables documentation has the details for your version.
Hosting platforms don't read your .env files from the repository, and they shouldn't, because those files are git-ignored. On Vercel you set each variable in the project settings and choose which environments it applies to: Production, Preview, Development or a combination. vercel env pull downloads the Development values into a local file, .env.local by default.
- A change to a
NEXT_PUBLIC_variable needs a new deployment, because the old bundle still holds the old string. - Preview deployments should get their own values, never production credentials, since every branch gets a preview.
- Variables are read at build time and runtime, so a server-only value used in a statically rendered page also needs a redeploy.
The Vercel environment variables documentation lists the current size limits and system variables such as the deployment URL.
| Symptom | Likely cause | Fix |
|---|---|---|
undefined in a Client Component | No NEXT_PUBLIC_ prefix, or a dynamic lookup | Add the prefix for public values; read it as process.env.NEXT_PUBLIC_X |
| Old value after changing it on the host | Inlined or statically rendered at build time | Redeploy, or render the code per request |
| Works locally, empty in tests | .env.local is skipped when NODE_ENV is test | Put test values in .env.test |
| A teammate's app crashes on start | Their .env.local lacks a key you added | Update .env.example and share values from one source |
| Secret visible in page source | Secret given the NEXT_PUBLIC_ prefix | Rotate it, then rename it without the prefix |
Validating the environment at startup turns the quiet failures into loud ones. A small module that reads every required variable once, checks it and exports typed values means a missing key fails the build or the first request with a clear message, instead of a blank page three clicks later.
.env.local is personal and git-ignored, which is right for secrets and wrong for coordination: when one developer adds RESEND_API_KEY, nobody else's file has it. The fix is one source of truth that everyone pulls from. With ProductEnv, link the file once and pull it whenever something changes:
npx productenv init
npx productenv link development .env.local
npx productenv pull
✅ Pulled 9 secrets from store/development → .env.local
# CI or a script: check for drift without revealing values
npx productenv diff --exit-codeThe link lives in .productenv.json, which you commit, so the next developer's pull writes .env.local too. Production values can go straight to Vercel with a deploy integration instead of being typed into its settings, and the VS Code extension shows which environments define each process.env.X your code reads. The guide to keeping .env files in sync covers the workflow in more depth.
Why is process.env undefined in a Next.js Client Component?
- Client Components only receive variables whose names start with
NEXT_PUBLIC_, and only when read literally asprocess.env.NEXT_PUBLIC_NAME. Variables without the prefix stay on the server, and dynamic lookups such asprocess.env[name]are not replaced at build time. Restartnext devafter editing a .env file so the new value is picked up. Should .env.local be committed in a Next.js project?
- No.
.env.localis meant for secrets and personal overrides, so it belongs in.gitignore. Commit a.env.examplewith the key names instead, and keep the real values in a secrets manager or your hosting platform. Can NEXT_PUBLIC_ variables change without rebuilding?
- No. They are replaced with their literal values during
next build, so the deployed JavaScript contains the old string until you build again. If a client-side value must change per environment without a rebuild, read it on the server at request time and pass it to the client as a prop. Which .env file does next build use?
next buildruns withNODE_ENVset toproduction, so it loads.env.production.local,.env.local,.env.productionand.env, with the real environment taking priority over all of them. Values for staging usually come from the hosting platform rather than a separate file.