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! 🙏
Top comments (0)