v1.7.0  ·  zero dependencies  ·  MIT  ·  Node ≥ 16

Your .env, validated
before it breaks production.

env-saaf checks your environment variables the moment your app starts — typed values, defaults, choices — and reports every problem at once, in plain language. No more silent config bugs at 2 a.m.

$ npm install env-saaf
npm version npm downloads license zero dependencies

Sound familiar?

Every value in process.env is a string, and nothing checks them for you. These bugs ship quietly and explode loudly.

💥

"false" is truthy

You set DEBUG=false — and the app still runs in debug mode, because the string "false" is truthy in JavaScript.

🔢

"3000" + 1 = "30001"

PORT arrives as a string. The first time your code does arithmetic on an env value, it silently concatenates instead.

🚨

Missing in production

A variable exists on your laptop but never made it to the server. The app crashes on boot — or worse, runs with undefined.

🔤

The silent typo

The file says DATABSE_URL. Your code reads DATABASE_URL, gets nothing, and nobody is ever told.

Declare a schema. Get typed values.

One small schema at startup replaces scattered process.env reads — and when something is wrong, you hear about all of it at once.

server.js
const { validate } = require('env-saaf');

const env = validate({
  PORT:    { type: 'port', required: true },
  DEBUG:   { type: 'boolean', default: false },
  API_KEY: { type: 'string', required: true },
  MODE:    { type: 'string',
             choices: ['development', 'production'],
             default: 'development' },
});

// env.PORT is a number. env.DEBUG is a real boolean.
app.listen(env.PORT);
when something is wrong
✗ EnvSaafError: 2 problems with your environment

  • PORT — expected a port (0–65535), got "abc"
  • API_KEY — missing (required)

Every problem is listed in one error —
fix them all in a single pass, instead of
crashing, fixing one, and crashing again.

Secret values are masked in the output,
so the error is safe to log and share.

Everything a growing app needs

Small surface, sharp edges. env-saaf does one job — trustworthy configuration — and does it completely.

🧮

Typed values

Strings become real numbers, booleans and ports. "false" becomes false; "3000" becomes 3000.

🧾

Every error at once

One EnvSaafError lists every missing, invalid and unexpected variable — no fix-one-crash-again loop.

📂

loadEnv built in

Read your .env file and validate it in a single call, with defaults applied and types converted.

🔒

Strict mode

Fail on variables that exist but are not in your schema, so a typo'd name can never hide in the file.

🖥️

A CLI that guards .env.example

check diffs your real file against the example; scan finds every process.env read in your code. Add --fix and check appends the missing keys itself.

🙈

Secrets stay secret

Values are masked in error messages and output — safe for logs, CI and screenshots.

⌨️

TypeScript inference

The result type is inferred from your schema: env.PORT is a number, not a string, in your editor too.

🪶

Zero dependencies

Nothing extra lands in your node_modules. Installs in a blink and audits clean, every time.

Try it right here

This little demo runs the same rules as the real package. Break something — a bad port, a missing key — and validate.

validate({ PORT: { type: 'port', required: true }, DEBUG: { type: 'boolean', default: false }, API_KEY: { type: 'string', required: true }, MODE: { type: 'string', choices: ['development', 'production'], default: 'development' } })

Clear API_KEY or type a bad PORT, then validate again — every problem shows up at once, just like the real thing.

Guard the example file, too

Most config bugs are really .env.example drift. The CLI keeps the example, the real file and your code in agreement.

npx env-saaf check

Compare your .env against .env.example and fail CI when they drift apart.

✗ missing: DB_URL ✗ extra: OLD_KEY
npx env-saaf scan

Scan your codebase for process.env reads and check that each one is declared.

✓ 14 reads found — all declared
npx env-saaf check --json

Machine-readable output for scripts and CI dashboards. Shipped in v1.2.0.

{"ok":false,"missing":["DB_URL"],"extra":["OLD_KEY"]}

Built in the open

env-saaf grows in public — and contributors are already shaping it. Thank you.

abdul-karim-mia
Our first outside contributor — fixed scanCode missing variables declared with let and var. Shipped in v1.1.0.
HarshRajSinghania
Added the --json flag for machine-readable CLI output. Shipped in v1.2.0.
Golden007-prog
Added the global --no-color CLI flag and seeded parser tests. Shipped in v1.2.0.
harshit3355
Added four copyable Recipes to the README. Shipped in v1.2.0.
bfs2021
Fixed loadEnv writing [object Object] for json values with assign: true. Shipped in v1.3.0.
theroimasters1-dev
Made loadEnv infer its return type from the schema, like validateEnv. Shipped in v1.3.0.
Your name here
Good first issues are open and labelled. Pick one, open a pull request, get credited on this page. Browse good first issues →

Questions

Zero dependencies — really?

Really. env-saaf has no runtime dependencies at all: what you install is the package itself, nothing else. That means faster installs, a smaller audit surface, and nothing transitive to break later.

How is this different from dotenv?

dotenv loads a file into process.env; env-saaf makes sure what is there is actually valid — typed, present and allowed. Use env-saaf's loadEnv to do both in one call, or keep dotenv and validate process.env right after it loads.

What happens when validation fails?

Your app stops at startup with one EnvSaafError listing every problem — missing, invalid and unexpected variables together. Failing loudly at boot beats failing quietly in production.

Does it work with TypeScript?

Yes. env-saaf ships its own type definitions, and the validated result is inferred from your schema — env.PORT is typed as a number, not a string.

Which Node versions are supported?

Node.js 16 and newer.