Next.js Environment Variables Explained
Next.js reads .env files from the project root into process.env. Variables are server-only by default. Prefix a name with NEXT_PUBLIC_ and Next.js copies its value into the browser JavaScript during next build, so it is public and frozen until you rebuild. Never put a secret behind that prefix.
| Order | Source | Notes |
|---|---|---|
| 1 | process.env | Values already set by the shell or host always win |
| 2 | .env.$(NODE_ENV).local | For example .env.development.local |
| 3 | .env.local | Skipped when NODE_ENV is test |
| 4 | .env.$(NODE_ENV) | For example .env.production |
| 5 | .env | Shared defaults for every environment |
Updated
Paste the contents of a .env file below. The checker sorts each variable into server only or sent to the browser, and flags public names that look like secrets. It runs entirely on this page; nothing you paste is uploaded.
.env checker
Paste .env lines
# Database
DB_HOST=localhost
DB_PASS=s3cret
DATABASE_URL=postgres://app:$DB_PASS@$DB_HOST/app
# Public
NEXT_PUBLIC_SITE_URL=https://example.com
NEXT_PUBLIC_STRIPE_SECRET_KEY=sk_test_1233 server only, 2 sent to the browser
| Variable | Available in | Note |
|---|---|---|
| DB_HOST | Server only | |
| DB_PASS | Server only | |
| DATABASE_URL | Server only | References DB_PASS, DB_HOST, which Next.js expands. |
| NEXT_PUBLIC_SITE_URL | Server and browser (inlined at build) | |
| NEXT_PUBLIC_STRIPE_SECRET_KEY | Server and browser (inlined at build) | Name looks like a secret. Anything with NEXT_PUBLIC_ is visible to every visitor. |
Reading variables in code
Server-only value in a Route Handler
DB_URL has no prefix, so it exists only on the server. Route Handlers and Server Components can read it freely.
// app/api/health/route.ts
export async function GET() {
const hasDb = Boolean(process.env.DB_URL);
return Response.json({ database: hasDb ? "configured" : "missing" });
}Public value in a Client Component
The full literal name is required. At build time Next.js swaps it for the value, so the browser sees a plain string.
"use client";
export default function Footer() {
return <a href={process.env.NEXT_PUBLIC_SITE_URL}>Home</a>;
}Read a value per request
Awaiting connection() makes the page render dynamically, so the value comes from the running server instead of the build.
import { connection } from "next/server";
export default async function Page() {
await connection();
const region = process.env.APP_REGION ?? "unknown";
return <p>Served from {region}</p>;
}Load the same files outside Next.js
loadEnvConfig applies the same load order, which keeps scripts and test setups in step with the app.
// env-config.ts
import { loadEnvConfig } from "@next/env";
loadEnvConfig(process.cwd());
What NEXT_PUBLIC_ really does
The prefix is an instruction to the bundler. During next build, every literal process.env.NEXT_PUBLIC_SOMETHING in your code is replaced with the value from the environment where the build ran, and that text ends up in the JavaScript files anyone can download.
That has two consequences. First, a public variable is never secret, no matter how the file is named. Second, it is frozen: promoting one build from staging to production carries the staging value with it.
Reading values at runtime
Server code can read any variable, public or not. To make sure a value is read per request rather than at build time, render the page dynamically, for example by awaiting connection() from next/server before you read process.env.
The browser cannot read server variables at all. If a Client Component needs a runtime value, fetch it from your own Route Handler or pass it in as a prop from a Server Component.
Guard against leaks
A helper that reads an API key can be imported into a Client Component by accident. Add import 'server-only' at the top of such files and the build fails if a client file pulls them in.
How it works
- Paste your .env lines into the box, or edit the sample.
- Read the table: each variable is marked server only or sent to the browser.
- Act on any warning, such as a NEXT_PUBLIC_ name containing KEY, SECRET or TOKEN.
- Rebuild after changing any NEXT_PUBLIC_ value, because the old value is baked into the bundle.
Tips
- Keep .env files in the project root, even when your code lives in a src folder. Next.js does not look for them inside src.
- Commit .env and .env.test with safe defaults if your team wants them, but never commit .env*.local files.
- Read public variables by their full literal name. process.env[name] or a destructured process.env is not replaced at build time.
- Need one Docker image for staging and production? Read server variables during dynamic rendering, and pass anything the browser needs down as props.
- Use loadEnvConfig from @next/env to load the same files in an ORM config, a script or a test setup outside Next.js.
Frequently asked questions
Why is my environment variable undefined in the browser?
Without the NEXT_PUBLIC_ prefix a variable only exists on the server, and Next.js replaces it with an empty string in client code. Add the prefix only if the value is safe to publish, then build again.
I changed a NEXT_PUBLIC_ value on my host and nothing happened. Why?
Public values are inlined when next build runs. A deployed bundle keeps the old value until you build again with the new one.
Does .env.local work in tests?
No. When NODE_ENV is test, Next.js skips .env.local so every test run gets the same values. Put test defaults in .env.test and personal overrides in .env.test.local.
Can one variable reference another?
Yes. Write $OTHER_NAME inside a value and Next.js expands it. If the value needs a literal dollar sign, escape it as \$.
What is NODE_ENV set to?
If you do not set it, next dev uses development and other commands use production. The allowed values are production, development and test.
Related: Next.js App Router, Next.js API routes, Next.js authentication, Next.js vs Node.js, Deploy a React app.
Build your site free or see all free tools. On We.Inc you can build your site free, with no card and no trial needed to start. AI building is on paid plans from $19 a month.
More in Guides
- Next.js Image Component: Sizes, Preload and Remote Images
- Next.js Middleware Examples, Updated for proxy.ts
- Next.js SEO Checklist (App Router)
- Next.js vs Node.js: How They Relate
- Next.js vs React: What Next Adds and When You Need It
- Nginx 502 Bad Gateway, and the Four Other Errors Nginx Throws Most
- nginx Load Balancing: upstream, Methods and Health Checks
- No-Code AI Agent Builder: A Buyer's Scorecard
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App