npm version GitHub Zero dependencies MIT License

env-saaf

Zero-dependency environment variable validator for Node.js.
Fail fast at startup, with typed values and every error listed at once.

npm install env-saaf

The bugs it kills

Every process.env value is a string. That one fact causes the same production bugs again and again:

"false" is truthy

Set DEBUG=false and your debug code still runs, because a non-empty string is truthy in JavaScript. env-saaf parses real booleans.

"3000" + 1 = "30001"

Ports and numbers arrive as strings, so arithmetic silently becomes concatenation. env-saaf returns typed numbers and validated ports (0–65535).

Missing in production

A forgotten variable does not fail at startup — it fails on the first request that needs it. env-saaf refuses to boot and lists all problems in one error.

Quick start

One schema, one call. The result is a frozen object with correctly typed values.

const { validateEnv } = require('env-saaf');

const env = validateEnv({
  PORT:      { type: 'port', required: true },
  DEBUG:     { type: 'boolean', default: false },
  API_URL:   { type: 'url', required: true },
  ADMIN_EMAIL: { type: 'email', required: true },
  LOG_LEVEL: { type: 'string', choices: ['info', 'warn', 'error'], default: 'info' },
});

// env.PORT is a number · env.DEBUG is a real boolean
// anything wrong? One EnvSaafError listing every problem.

Keep .env.example honest

Your code and your .env.example drift apart silently. The CLI catches it — made for CI.

npx env-saaf scan ./src

Lists every env var your code actually uses — dot access, bracket access and destructuring.

npx env-saaf check .

Diffs code usage against .env.example — and with --schema, validates the real values too (use --values for another environment file). Exit code 1 if anything used is undocumented — or add --fix and let check append the missing keys for you.

npx env-saaf example ./src

Drafts a fresh .env.example straight from a code scan.

npx env-saaf print . --schema ./env.schema.js

Prints the effective configuration — coerced values and defaults — with sensitive rules and secret-named keys masked; --reveal shows the real values.

npx env-saaf init .

Scaffolds a starter env.schema.js and a matching .env.example, or infers the schema from an existing example with --from-example.

maskSecrets(process.env)

Log your config safely — secrets print masked (sk***56), never in full.

Versions

Built in the open, one honest step at a time.

v0.1.0

Core validation

validateEnv with typed coercion — string, number, boolean, url, port — plus required, defaults and choices. All errors collected at once.

v0.2.0

Files, scanning, CLI

.env parsing, code scanning for process.env usage, used-vs-documented diff, and the env-saaf check / scan CLI with CI exit codes.

v1.0.0

Stable 1.0

Secret masking, .env.example generation from schema and from code scans, TypeScript definitions, stable API.

v1.1.0

Community release

Ten fixes and features: loadEnv, strict mode, boolean on/off + y/n, inline comments in .env files, scanCode let/var support — including the project's first outside contribution.

v1.2.0

Types and tooling

Email type and pattern option, CLI --json output, per-key transform, ${VAR} interpolation, multi-file loadEnv, int type with min/max, and README recipes.

v1.3.0

More types, safer secrets

json, uuid and hex types, minLength/maxLength and hex length options, trim: false, loadEnv return-type inference, safer .env.example generation for secret keys — with two more outside contributions.

v1.4.0

Three more types, sharper CLI

ipv4, semver and base64 types, check now compares real .env keys, global --version, example --write, scan reports unused keys, and check exits 1 on every finding — with a new outside contributor.

v1.5.0

Two more types, calmer errors

duration and host types, date and file types, non-throwing mode, ${VAR:-default} interpolation, CLI --schema validation, and duplicate-key errors in .env files — with eight outside contributions.

v1.6.0

Bigint, groups, sharper CLI

bigint and ipv6 types, grouped nested schemas, the array type, requiredIf/aliases/conflictsWith options, loadEnvAsync, toJsonSchema export, init and print commands, check --fix, and real-secret detection in check — the biggest release yet, with many outside contributions.

v1.7.0

JWT type, stricter schemas

jwt type for JSON Web Token shape validation, stricter schemas (non-array choices and length off hex rejected up front), layered loadEnv interpolation, scanCode alias destructuring, and masked non-string secrets.