# Environment Variables

> Securing secrets and exposing public keys.

Subject: Next.js | Lesson 60 of 65 | 3 min read
Interactive lesson: https://codewithanimation.in/nextjs/environment-variables
Previous: [Middleware Capabilities](https://codewithanimation.in/nextjs/middleware-capabilities.md) | Next: [Edge vs Node.js Runtime](https://codewithanimation.in/nextjs/edge-vs-node-runtime.md)

## The NEXT_PUBLIC Prefix

Next.js seamlessly loads `.env`, `.env.local`, `.env.development`, and `.env.production` files into Node's `process.env`.

By default, all environment variables are only available in the Node.js/Edge environments (Server Components, Route Handlers, Middleware).

To expose a variable to the browser (Client Components), you must explicitly prefix it with `NEXT_PUBLIC_`.

## Preventing Catastrophic Leaks

If you accidentally bundle your `STRIPE_SECRET_KEY` or `DATABASE_URL` into the client's JavaScript, a malicious user can extract it by opening the browser DevTools.

Next.js enforces security by default. It physically strips non-public variables from the client bundle during compilation, replacing them with `undefined`.

## Implementation

```env
# .env.local
STRIPE_SECRET_KEY=sk_test_12345
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_12345
```

```typescript
// Server Component (app/page.tsx)
export default function Page() {
  // This works perfectly on the server.
  const secret = process.env.STRIPE_SECRET_KEY;
  
  return <ClientComponent publishableKey={process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY} />
}
```

```typescript
// Client Component (components/ClientComponent.tsx)
'use client';
export default function ClientComponent() {
  // This will be undefined in the browser!
  const secret = process.env.STRIPE_SECRET_KEY; 
  
  // This works fine.
  const pubKey = process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY;
}
```

## Common mistake: The Undefined Trap

If you try to read a private environment variable in a Client Component, Next.js does not throw a loud compilation error. Instead, the value is simply evaluated as `undefined`.

This often leads to obscure runtime errors like `TypeError: Expected string, received undefined` when passing the missing credential to an SDK like Firebase or Supabase.

## Try it: Environment Variable Compiler

Watch how the Next.js compiler treats private vs public environment variables during a build.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/environment-variables

## Check yourself

### Question 1

What happens if you try to read process.env.DATABASE_URL inside a Client Component ('use client')?

- A. Next.js throws a strict compilation error.
- B. It evaluates to undefined, potentially causing runtime crashes.
- C. It successfully reads the value, leaking it to the browser.
- D. It falls back to the .env.production file.

Correct answer: B

Next.js replaces any environment variable that doesn't start with NEXT_PUBLIC_ with undefined during the client build step. This prevents security leaks but often causes unexpected undefined runtime errors.

- Why not A: Next.js does not throw a build error; it simply replaces the variable with undefined in the bundle.
- Why not C: Next.js prevents this security leak by stripping it from the client bundle.

## Remember this

- Always use the `NEXT_PUBLIC_` prefix for variables the browser needs (e.g., Supabase anon key, Stripe publishable key).
- Never use `NEXT_PUBLIC_` for database passwords, API secrets, or service account keys.
- `.env.local` is for local secrets and should be added to `.gitignore`.
