env (next.config) & environment variables

Inlines environment variables at build time and exposes public ones.

Since Next 9.4+ (.env support) Spec ↗

Syntax

env: { KEY: process.env.KEY } // or NEXT_PUBLIC_* in .env

Parameters

NameTypeRequiredDescription
env Record<string, string> No Build-time variables inlined into the bundle.

Returns

NextConfig — Inlined env values; merged with .env files.

Examples

# .env.local
DATABASE_URL=postgres://localhost/app
NEXT_PUBLIC_API_URL=https://api.example.com
// Server-only: available in Server Components/actions/handlers
const db = connect(process.env.DATABASE_URL)

// Client-exposed: must be prefixed NEXT_PUBLIC_
export function ApiBadge() {
  return <span>{process.env.NEXT_PUBLIC_API_URL}</span>
}
// next.config.js — legacy explicit inlining
const nextConfig = {
  env: {
    BUILD_ID: process.env.BUILD_ID,
  },
}
module.exports = nextConfig

Notes

Variables in `.env*` files are loaded automatically. Only those prefixed `NEXT_PUBLIC_` are inlined into client bundles; everything else is server-only — never put secrets in `NEXT_PUBLIC_*`. Values are inlined at build time, so changing them requires a rebuild. Use `.env.local` for secrets (gitignored).

See also