
Stop Writing Environment Variable Validation From Scratch
Every project I've worked on has this code somewhere. const PORT = process . env . PORT const DATABASE_URL = process . env . DATABASE_URL const API_KEY = process . env . API_KEY if ( ! DATABASE_URL ) throw new Error ( ' DATABASE_URL is required ' ) if ( ! API_KEY ) throw new Error ( ' API_KEY is required ' ) const port = parseInt ( PORT ?? ' 3000 ' ) It's not terrible. But it's not great either. It's scattered across files, it gives you string | undefined everywhere, and when something is wrong you get a cryptic error 10 layers deep in your app instead of a clear message at startup. So you clean it up a bit. Maybe you centralize it. Maybe you add some type assertions. Maybe you reach for envalid or t3-env . And then you discover: envalid has no TypeScript inference — everything comes back as a loose type t3-env is great but requires Zod, and not every project uses Zod Neither works cleanly across Node, Vite, Deno, and Cloudflare Workers without config gymnastics So you end up solving i
Continue reading on Dev.to
Opens in a new tab


