Simple, framework-agnostic SDK for validating required environment variables in Node.js apps. Keep configs clean, fail fast with friendly, well-formatted logs.
You ship a service that runs perfectly locally and on staging, but production crashes or—worse—behaves unpredictably because:
- A required variable (like
DATABASE_URL) wasn’t set in the production/local/staging environment PORTwas set to"eighty"instead of a numberADMIN_EMAILhad a typoBASE_URLmissed its protocol and broke redirects- New feature depends on a fresh env var; a team mate forgot to include it in prod environment. All quiet at boot… then the first request hits that path and kaboom — hello 500s.
Env Preflight performs a quick preflight at startup, validating the presence and basic types of the environment variables your app relies on. When something’s off, it prints a clear, friendly report and (optionally) exits fast so you don’t ship broken configs. This saves hours of debugging and prevents subtle runtime bugs caused by misconfigured environments in local dev, CI, and production.
- Zero coupling to dotenv (or any loader) – you bring envs, Env Preflight validates
- Simple rules: 'string' | 'number' | 'boolean' | 'url' | 'email'
- Clean output with minimal color (or plain mode)
- Fail-fast option to exit process on invalid/missing envs
- Tiny surface area and TypeScript types out of the box
npm install env-preflight
# or
yarn add env-preflight
# or
pnpm add env-preflightimport { EnvGuard, EnvGuardConfig } from "env-preflight";
const config: EnvGuardConfig = {
required: {
API_KEY: "string",
PORT: "number",
DATABASE_URL: "url",
ADMIN_EMAIL: "email",
DEBUG_MODE: "boolean",
},
options: {
exitOnFail: true,
logStyle: "color",
},
};
new EnvGuard(config).validate();const { EnvGuard } = require("env-preflight");
const config = {
required: {
API_KEY: "string",
PORT: "number",
DATABASE_URL: "url",
ADMIN_EMAIL: "email",
DEBUG_MODE: "boolean",
},
options: {
exitOnFail: true,
logStyle: "color",
},
};
new EnvGuard(config).validate();import dotenv from "dotenv";
dotenv.config();
import { EnvGuard } from "env-preflight";
new EnvGuard({ required: { API_KEY: "string" } }).validate();Env Preflight only reads process.env. Load envs however you prefer (dotenv, shell, CI vars, Docker, etc.).
- required: map of env var names to rule strings
- Allowed rules:
'string' | 'number' | 'boolean' | 'url' | 'email' - Note on enums: treat enums as strings in phase 1; enforce allowed values in your app logic
- Important: use string literals for rules. Do not pass TypeScript types like
string/number.
- Allowed rules:
- options (optional):
exitOnFail(boolean): exit process with code 1 when invalid – defaulttruelogStyle("color" | "plain"): colorized or plain output – defaultcolor
Default options:
{
exitOnFail: true,
logStyle: 'color'
}validate()checks each required env var:- Missing → error collected
- Present but wrong type → error collected
- If there are errors:
- A friendly block is printed
- If
exitOnFailistrue, the process exits with code1
- Returns
truewhen valid,falseotherwise
Color style (colors not rendered here):
[env-preflight] Oops… preflight checks failed
The following environment variables need attention:
- ✖ Missing required environment variable: "DATABASE_URL"
- ✖ Environment variable "PORT" must be a valid number.
- ✖ Environment variable "ADMIN_EMAIL" must be a valid email.
Action: Update your environment (.env, shell, CI) and restart the app.
Status: Not cleared for takeoff 🚫🚀
Plain style:
[env-preflight] Oops… preflight checks failed
The following environment variables need attention:
- ✖ Missing required environment variable: "DATABASE_URL"
- ✖ Environment variable "PORT" must be a valid number.
- ✖ Environment variable "ADMIN_EMAIL" must be a valid email.
Action: Update your environment (.env, shell, CI) and restart the app.
Status: Not cleared for takeoff 🚫🚀
Success message:
[env-preflight] ✅ Environment variables validated successfully, server is cleared for takeoff! 🚀
- Load envs early (before creating your server/app)
- Keep secrets out of source control; use
.envfor local dev and CI secrets for pipelines - Run Env Preflight in CI to catch misconfigurations before deploy
- Node.js: 14+ recommended
- Module format: CommonJS build
- Colors: Uses
chalk@4(CommonJS). If your app is pure ESM and you preferchalk@5, migrate your project to ESM.
class EnvGuard {
constructor(config: EnvGuardConfig);
validate(): boolean;
}
type ValidationRule = "string" | "number" | "boolean" | "url" | "email";
interface IOptions {
exitOnFail: boolean;
logStyle: "color" | "plain";
}
interface EnvGuardConfig {
required: Record<string, ValidationRule>;
options?: IOptions;
}- “Why isn’t
{ PORT: number }working?”- Rules must be string literals (e.g.,
{ PORT: 'number' }). TypeScript types do not exist at runtime.
- Rules must be string literals (e.g.,
- “What counts as a boolean?”
- Accepted:
"true" | "false" | "1" | "0" | "yes" | "no"(case-insensitive)
- Accepted:
- “Do I need dotenv?”
- No. EnvGuard only reads
process.env. Use any loader or deployment env.
- No. EnvGuard only reads
Issues and PRs are welcome.
ISC © Igomigo Fatai Victor