env-shield-pro
v3.0.4
Published
Safe environment variable validation for Node.js
Maintainers
Keywords
Readme
env-shield-pro
Zero-dependency env variable validation for Node.js (ESM).
Validate, coerce, and fail fast — before production breaks.
Install
npm install env-shield-proQuick Start
import { env } from "env-shield-pro";
const PORT = env.PORT.number(); // "3000" → 3000
const SECRET = env.JWT_SECRET.required(); // throws if missing
const NODE_ENV = env.NODE_ENV.enum(["dev", "prod"]);
const IS_ADMIN = env.IS_ADMIN.boolean(); // "true" → true
const DB_URL = env.DATABASE_URL.url();
const EMAIL = env.ADMIN_EMAIL.email();
const TIMEOUT = env.TIMEOUT.default(5000); // fallback with warningCLI
npx env-shield-pro check # validate all env vars are set
npx env-shield-pro check --ci # exit 0 (pass) / 1 (fail) for CI
npx env-shield-pro scan # list every env var used in project
npx env-shield-pro generate # create .env.example from your codeCI Usage (GitHub Actions)
- run: npx env-shield-pro check --ciExits 0 if all variables are set, 1 otherwise. Fail your build when config drifts.
API
| Method | Returns | Throws if |
|--------|---------|-----------|
| .required() | string | missing / empty / null |
| .number() | number | not numeric or missing |
| .boolean() | boolean | not "true" / "false" |
| .enum([...]) | string | not in list or missing |
| .default(v) | string \| v | never — warns on fallback |
| .url() | string | invalid URL or missing |
| .email() | string | invalid email or missing |
Error Colors
❌ Missing required env variable: JWT_SECRET ← red (fatal)
⚠ TIMEOUT not set, using default: 5000 ← yellow (warning)import { env, warn, debug } from "env-shield-pro";Why this?
| | process.env | env-shield-pro |
|---|---|---|
| Missing vars | undefined silently | ❌ clear error at startup |
| Type coercion | manual | .number(), .boolean() |
| Validation | manual | .url(), .email(), .enum() |
| Defaults | manual | .default() |
| CI-ready | ❌ | npx env-shield-pro check --ci |
| Size | — | ~2 KB, zero deps |
License
MIT © Karthik
