Next.js .env Generator
Next.js reads .env.local automatically — no import needed. This generator produces a complete file for the most common Next 15 stack: NextAuth for auth, Supabase for data, Stripe for payments.
NEXT_PUBLIC_SITE_URL=http://localhost:3000 DATABASE_URL=postgresql://user:password@localhost:5432/mydb NEXTAUTH_URL=http://localhost:3000 NEXTAUTH_SECRET=OHzfOlfmca8e9/aleogIBkpbCFUE4qucEHGGKrjcSx0= NEXT_PUBLIC_SUPABASE_URL= NEXT_PUBLIC_SUPABASE_ANON_KEY= SUPABASE_SERVICE_ROLE_KEY= STRIPE_SECRET_KEY=sk_test_ STRIPE_WEBHOOK_SECRET=whsec_ NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_
Secrets regenerate on every page load. Want to combine stacks? Use the full generator.
How to use this .env in Next.js
- 1.Paste the output into .env.local in your project root.
- 2.Keep NEXT_PUBLIC_ prefixes — those variables are exposed to the browser.
- 3.Restart the dev server after adding or changing variables.
- 4.Add all the same keys in your Vercel (or host) project settings for production.
Gotchas
- ⚠Variables without NEXT_PUBLIC_ are server-only. Accessing them in a client component returns undefined.
- ⚠.env.local is loaded in every env except during tests. Use .env.test for test overrides.
- ⚠Next.js inlines NEXT_PUBLIC_ variables at build time — changing them requires a rebuild.
Common keys explained
NEXT_PUBLIC_SITE_URLCanonical URL — used for absolute redirects, OG tags, sitemaps.
DATABASE_URLPostgres or Supabase connection string.
NEXTAUTH_SECRETRequired. Sign JWTs + encrypt session cookies.
NEXT_PUBLIC_SUPABASE_URLYour Supabase project URL. Safe to expose.
NEXT_PUBLIC_SUPABASE_ANON_KEYPublic Supabase key. Safe to expose — RLS protects data.
SUPABASE_SERVICE_ROLE_KEYServer-only admin key. Never prefix with NEXT_PUBLIC_.
STRIPE_SECRET_KEYServer-only Stripe secret. Test-mode in dev, live-mode only in prod.
STRIPE_WEBHOOK_SECRETVerifies Stripe webhook signatures. Rotate per environment.
Loading and validating the .env in Next.js
// Next.js loads .env, .env.local, .env.development(.local) by itself.
// Validate once at startup so a missing key fails the build, not a request.
import { z } from "zod";
const server = z.object({
DATABASE_URL: z.string().url(),
NEXTAUTH_SECRET: z.string().min(32),
STRIPE_SECRET_KEY: z.string().startsWith("sk_"),
});
const client = z.object({
NEXT_PUBLIC_SITE_URL: z.string().url(),
NEXT_PUBLIC_SUPABASE_URL: z.string().url(),
});
export const env = {
...server.parse(process.env),
// NEXT_PUBLIC_ values must be referenced literally to be inlined
...client.parse({
NEXT_PUBLIC_SITE_URL: process.env.NEXT_PUBLIC_SITE_URL,
NEXT_PUBLIC_SUPABASE_URL: process.env.NEXT_PUBLIC_SUPABASE_URL,
}),
};Next.js: env vars in development, staging and production
Next.js loads files in a fixed order: .env.$(NODE_ENV).local, then .env.local, then .env.$(NODE_ENV), then .env. The first file that defines a key wins. .env.local is ignored when NODE_ENV=test, so tests stay reproducible.
NEXT_PUBLIC_ variables are copied into the JavaScript bundle at build time. If you build one Docker image and deploy it to staging and production, both get the NEXT_PUBLIC_ values that were present during next build. Changing them at runtime does nothing. Server-only variables are read at request time and can differ per environment.
Next.js: .env errors and fixes
NEXT_PUBLIC_* is undefined in the browserThe variable was added after the dev server started, or it is read with dynamic access like process.env[name]. Restart next dev and reference it literally: process.env.NEXT_PUBLIC_SITE_URL.
[next-auth][error][NO_SECRET]NEXTAUTH_SECRET (AUTH_SECRET in Auth.js v5) is missing in production. Add it in your host's environment settings, not only in .env.local, which is never deployed.
Stripe: No API key providedSTRIPE_SECRET_KEY is read in a client component. Secret keys only exist on the server: move the call into a Route Handler or Server Action.
Next.js .env FAQ
What is the difference between .env and .env.local in Next.js?
.env holds defaults that are safe to commit. .env.local holds machine-specific secrets, overrides .env, and belongs in .gitignore. Both are loaded automatically.
Can I change NEXT_PUBLIC_ variables without rebuilding?
No. They are inlined at build time. For per-environment public config, render the value from a server component, or expose it from an API route and read it at runtime.
Is it safe to put the Supabase anon key in NEXT_PUBLIC_?
Yes. The anon key is designed to be public and is limited by Row Level Security. The service role key bypasses RLS and must never get a NEXT_PUBLIC_ prefix.