More Next.js answers

by Danny
Published 2 min read

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
_10
NEXT_PUBLIC_API_URL=https://api.example.com


_10
'use client';
_10
_10
export 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 from process.env is not replaced and stays undefined.

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
_10
const { NEXT_PUBLIC_API_URL } = process.env;
_10
const key = 'NEXT_PUBLIC_API_URL';
_10
const url = process.env[key];
_10
_10
// Works
_10
const 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)
_10
import { Forecast } from './forecast';
_10
_10
export 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.local to load in tests. Next.js skips .env.local when NODE_ENV is test; use .env.test instead.
  • Committing .env.local. Keep it in .gitignore and share an .env.example with 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.config and assuming the browser sees the same. The config runs in Node.js at build time.
#

Was this resource helpful?