今日已更新 339 条资讯 | 累计 19899 条内容
关于我们

I built a zero-dependency TypeScript env validator

giannielloemmanuele-lgtm 2026年06月29日 17:38 4 次阅读 来源:Dev.to

Every Node.js developer has been burned by this at least once: const port = parseInt ( process . env . PORT ); // NaN if PORT is missing const db = process . env . DATABASE_URL ; // string | undefined — not safe! Your app starts fine locally, then crashes in production because someone forgot to set an env var. The error shows up 3 hours later, not at startup. The solution I built @harmand66/typesafe-env — a tiny, zero-dependency library that validates and types your environment variables at boot time. import { createEnv } from ' @harmand66/typesafe-env ' ; const env = createEnv ({ PORT : { type : ' number ' , default : 3000 }, DATABASE_URL : { type : ' string ' , required : true }, DEBUG : { type : ' boolean ' , default : false }, }); // ✅ TypeScript knows PORT is a number env . PORT + 1 // 3001 — not "30001" env . DATABASE_URL // string — guaranteed, never undefined If anything is missing or wrong, your app fails immediately at startup with a clear message: All errors at once — no more fixing them one by one. Why not Zod? Zod is great but it's 57kb and requires a lot of boilerplate for this specific use case. @harmand66/typesafe-env is zero dependencies and does one thing well. Try it npm install @harmand66/typesafe-env GitHub: https://github.com/giannielloemmanuele-lgtm/typesafe-env npm: https://www.npmjs.com/package/@harmand66/typesafe-env Would love any feedback or contributions! 🙏

本文内容来源于互联网,版权归原作者所有
查看原文