DEV Community

Nishant Bhatte
Nishant Bhatte

Posted on

I built a zero-dependency env variable validator for Node.js — env-guard

Every Node.js project reads from process.env. But process.env gives you raw strings — no types, no validation, no defaults.

This leads to silent bugs like:

  • PORT is undefined and your app crashes
  • ENABLE_CACHE === "false" is truthy in JS
  • Missing DATABASE_URL discovered at runtime, not startup

I got tired of writing the same boilerplate fix in every project, so I built env-guard.

What it does

  • Zero dependencies — pure Node.js
  • Type coercion — strings become numbers, booleans, parsed JSON
  • Collects ALL errors at once — see every problem in one error
  • Beautiful error output — with hints and examples in the terminal
  • Built-in .env loader — no dotenv needed
  • Auto-generates .env.example from your schema

How it works

Install it:
npm install @ironfighter23/env-guard

Define your schema once:

const { guard, schema } = require('@ironfighter23/env-guard');

const config = guard({
PORT: schema.port().default(3000),
DATABASE_URL: schema.url().sensitive(),
NODE_ENV: schema.enum(['development', 'production']),
SECRET_KEY: schema.string().min(32),
ENABLE_CACHE: schema.boolean().default(false),
});

If anything is wrong you see ALL errors at once with hints — not one at a time.

Links

Would love your feedback!

Top comments (0)