Loading content…
Loading content…
Understand why .env files matter and how to use them safely in production
Senior Developer Wisdom
# .env.local (development)
DATABASE_URL=postgres://user:pass@localhost:5432/app
JWT_SECRET=replace-me
API_BASE_URL=http://localhost:3001
NEXT_PUBLIC_SITE_URL=http://localhost:3000
// Server-only
const dbUrl = process.env.DATABASE_URL;
const jwtSecret = process.env.JWT_SECRET;
// Client-safe (must be prefixed)
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL;
NEXT_PUBLIC_ are exposed to the browser.Common Pitfall
# .env.example
DATABASE_URL=
JWT_SECRET=
NEXT_PUBLIC_SITE_URL=
const required = ["DATABASE_URL", "JWT_SECRET"];
for (const key of required) {
if (!process.env[key]) {
throw new Error(`Missing env var: ${key}`);
}
}
.env Best Practices
.env files keep secrets out of source controlNEXT_PUBLIC_ only for client-safe values.env.exampleMarking it complete updates your roadmap progress percentage.