{"content":"# Environment Variable Management with better-env + Vercel\n\nSync local env files with Vercel environments using better-env pull/load commands while preserving local overrides.\n\n### Managing Environment Variables with better-env + Vercel\n\nUse `better-env` to sync local `.env` files with Vercel environments.\n\n### Install and configure\n\n```bash\nbun add better-env\nbun run env:init\n```\n\nCreate `better-env.ts` in your project root:\n\n```typescript\nimport { defineBetterEnv, vercelAdapter } from \"better-env\";\n\nexport default defineBetterEnv({\n  adapter: vercelAdapter(),\n  environments: {\n    development: { envFile: \".env.development\", remote: \"development\" },\n    preview: { envFile: \".env.preview\", remote: \"preview\" },\n    production: { envFile: \".env.production\", remote: \"production\" },\n    test: { envFile: \".env.test\", remote: null },\n  },\n});\n```\n\n### Add scripts\n\n```json\n{\n  \"scripts\": {\n    \"env:init\": \"bunx --bun better-env init -y\",\n    \"env:pull\": \"bunx --bun better-env pull --environment=development\",\n    \"env:push\": \"bunx --bun better-env load .env.development --environment=development --upsert\",\n    \"env:pull:prod\": \"bunx --bun better-env pull --environment=production\",\n    \"env:push:prod\": \"bunx --bun better-env load .env.production --environment=production --upsert\"\n  }\n}\n```\n\n### Recommended file strategy\n\n```text\n.env.development  <- shared development values from Vercel\n.env.production   <- shared production values from Vercel\n.env.local        <- local-only overrides (never synced)\n```\n\n`better-env pull` writes to your configured env file and keeps `.env.local` untouched.\n\n### Daily workflow\n\n1. Pull shared vars: `bun run env:pull`\n2. Add local overrides in `.env.local`\n3. Update remote vars with `bunx --bun better-env upsert` or `bun run env:push`\n4. Validate before building: `bun run env:validate`\n\nFor scripts/config files that run outside Next.js runtime (for example Drizzle config), continue using `loadEnvConfig(process.cwd())` before reading env values."}