Assert required environment variables with typed results and clear, aggregated errors.
Zero runtime dependencies. TypeScript-first.
npm install env-assertimport { assertEnv, EnvAssertError } from "env-assert";
const env = assertEnv({
PORT: { type: "int", min: 1, max: 65535, default: 3000 },
DATABASE_URL: { type: "url" },
NODE_ENV: { type: "enum", values: ["development", "test", "production"] as const },
DEBUG: { type: "bool", default: false },
OPTIONAL_FLAG: { type: "string", required: false },
});
// env.PORT: number
// env.DATABASE_URL: string
// env.NODE_ENV: "development" | "test" | "production"If anything is missing or invalid, assertEnv throws EnvAssertError listing all problems (not just the first).
| Type | Notes |
|---|---|
string (default) |
non-empty when required |
int / float |
optional min / max |
bool |
accepts true/false/1/0/yes/no/on/off |
url |
must parse with new URL() |
enum |
values: readonly string[] |
required: false or a default makes a key optional.
MIT © Oliver Massaad