---
title: "Why is my Next.js environment variable undefined in the browser?"
description: "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."
url: https://www.danny.engineering/answer/nextjs/why-is-my-nextjs-environment-variable-undefined-in-the-browser
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:25:00.000Z
tags: ["Next.js"]
---

# Why is my Next.js environment variable undefined in the browser?

## 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.


```bash
# .env.local
NEXT_PUBLIC_API_URL=https://api.example.com
```


```typescript
'use client';

export function Status() {
  return <p>Talking to {process.env.NEXT_PUBLIC_API_URL}</p>;
}
```


## 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


```typescript
// Does not work in the browser
const { NEXT_PUBLIC_API_URL } = process.env;
const key = 'NEXT_PUBLIC_API_URL';
const url = process.env[key];

// Works
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.


```typescript
// app/weather/page.tsx (Server Component)
import { Forecast } from './forecast';

export default async function Page() {
  const res = await fetch('https://api.weather.example/today', {
    headers: { Authorization: `Bearer ${process.env.WEATHER_API_KEY}` },
  });
  const forecast = await res.json();
  return <Forecast data={forecast} />;
}
```


### 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.

## Related reading

- [Next.js: Environment variables](https://nextjs.org/docs/app/guides/environment-variables)
