Environment variables work on the server but are undefined in Client Components. Learn how the NEXT_PUBLIC_ prefix works, when values are inlined, and how to keep secrets safe.
The short answer
#Next.js only sends environment variables to the browser if their names start with NEXT_PUBLIC_. Everything else stays on the server, so process.env.API_URL is undefined in a Client Component. Rename the variable, restart the dev server, and rebuild for production.
_10# .env.local_10NEXT_PUBLIC_API_URL=https://api.example.com
_10'use client';_10_10export function Status() {_10 return <p>Talking to {process.env.NEXT_PUBLIC_API_URL}</p>;_10}
Why it happens
#There is no process.env in the browser. When Next.js builds your app, it finds every reference to process.env.NEXT_PUBLIC_SOMETHING in client code and replaces it with the literal value. After the build, the bundle contains the string "https://api.example.com", not a lookup.
This is deliberate. Your .env files often hold database passwords and API secrets. If every variable were sent to the browser, anyone could read them in the page source. The prefix is how you opt in, one variable at a time.
Two consequences follow from inlining:
- The value is fixed when you run
next build. Changing it on the server afterwards does nothing until you build again. - Only exact, static references are replaced. Code like
process.env[name]or destructuring fromprocess.envis not replaced and staysundefined.
How to fix it
#Add the prefix and restart
Rename the variable to start with NEXT_PUBLIC_, update the references, then stop and start next dev. Next.js reads .env files on start-up, so edits are not picked up while it is running. On your host, such as Vercel, set the variable in the project settings and trigger a new deployment.
Reference each variable directly
_10// Does not work in the browser_10const { NEXT_PUBLIC_API_URL } = process.env;_10const key = 'NEXT_PUBLIC_API_URL';_10const url = process.env[key];_10_10// Works_10const apiUrl = process.env.NEXT_PUBLIC_API_URL;
If you want a single config object, build it from direct references in one module and import that module where you need it.
Read secrets on the server and pass the result down
If the value is a secret, do not add the prefix. Use it in a Server Component, Server Action or Route Handler, and pass only the safe result to the client.
_10// app/weather/page.tsx (Server Component)_10import { Forecast } from './forecast';_10_10export default async function Page() {_10 const res = await fetch('https://api.weather.example/today', {_10 headers: { Authorization: `Bearer ${process.env.WEATHER_API_KEY}` },_10 });_10 const forecast = await res.json();_10 return <Forecast data={forecast} />;_10}
Use runtime values when one build runs in many environments
If you build a single Docker image and deploy it to staging and production, inlined values will not change between them. Read non-secret config on the server at request time and pass it to the client as props. Server Components rendered dynamically read process.env when the request arrives, not at build time.
Common pitfalls
#- Prefixing a secret just to make the error go away. Anything with
NEXT_PUBLIC_is visible to every visitor. - Expecting
.env.localto load in tests. Next.js skips.env.localwhenNODE_ENVistest; use.env.testinstead. - Committing
.env.local. Keep it in.gitignoreand share an.env.examplewith placeholder values. - Wrapping the value in quotes inside your hosting dashboard. Some dashboards keep the quote characters as part of the value.
- Checking the value in
next.configand assuming the browser sees the same. The config runs in Node.js at build time.
Related reading
#Was this resource helpful?