Environment variables

Config values fed to your app from outside the code, so the same build behaves differently on your laptop and in production.

the .env thingconfig that lives outside the codeenv varsenviroment variableswhere do I put my API keysettings that change between local and livedotenv fileprocess.env stuff

See it

Live demo coming soon

What it is

Environment variables are named values handed to your app by whatever is running it, rather than written into the source. Same build, different DATABASE_URL: localhost on your laptop, the staging cluster in staging, the real thing in production. Locally they come from a gitignored .env file; in production they come from your host's dashboard or a secrets manager. This is factor three of the twelve-factor app, and it is why one artifact can be promoted from staging to production untouched, as long as everything that differs is read at runtime.

Two kinds live in the same file and behave completely differently. Server-side vars stay on the machine. Client-side vars, the ones prefixed NEXT_PUBLIC_ or VITE_ or PUBLIC_, get inlined into the JavaScript bundle at build time and are readable by anyone with devtools. The prefix is a warning label, not a feature.

Gotchas that bite everyone: every value is a string, so 'false' is truthy and needs parsing. Client-side vars bake in at build time, so changing one in a dashboard does nothing until you rebuild. And a missing var usually fails silently as undefined, deep inside a request, at 2am. Validate the whole environment once at startup with Zod or envalid and crash loudly if something is absent. Keep a committed .env.example listing every key with fake values so the next person can actually run the thing.

Ask AI for it

Set up typed environment variable handling for this project. Create an env module that defines a Zod schema for every variable the app needs, splitting server-only vars from public client vars, parses process.env once at startup, and throws a readable error listing every missing or malformed key. Export the parsed object and replace all direct process.env reads with it. Coerce booleans and numbers rather than leaving them as strings. Also generate a committed .env.example with every key and placeholder values, and make sure .env and .env.local are gitignored.

You might have meant

secrets managementenvironment parityproduction environmentstaging environmentinfrastructure as code