A lightweight, type-safe environment variable validator for Node.js and TypeScript.
envfy validates, parses, and infers the types of your environment variables at application startup, helping your application fail fast when configuration is invalid.
- String validation
- Number parsing and validation
- Boolean parsing and validation
- Enum validation
- Optional environment variables
- Default values
- Aggregated validation errors
- Automatic TypeScript type inference
- Zero runtime dependencies
- Lightweight and dependency-free
- Strong TypeScript support with inferred return types
- Simple, declarative schema syntax
- Fail-fast configuration validation
- Suitable for small projects and production applications
npm install @yasserfedsi/envfyimport { validateEnv } from "@yasserfedsi/envfy";
const env = validateEnv({
PORT: "number",
DATABASE_URL: "string",
DEBUG: "boolean",
});
console.log(env.PORT); // number
console.log(env.DATABASE_URL); // string
console.log(env.DEBUG); // booleanTypeScript automatically infers the correct return types.
const env = validateEnv({
PORT: "number",
DATABASE_URL: "string",
DEBUG: "boolean",
});const env = validateEnv({
NODE_ENV: ["development", "production", "test"] as const,
});If .env contains:
NODE_ENV=stagingAn error is thrown:
NODE_ENV must be one of: development, production, test
const env = validateEnv({
API_KEY: {
type: "string",
optional: true,
},
});Missing optional variables are ignored.
If present, they are validated normally.
const env = validateEnv({
PORT: {
type: "number",
default: 3000,
},
HOST: {
type: "string",
default: "localhost",
},
DEBUG: {
type: "boolean",
default: false,
},
});If the environment variables are missing, the default values are returned.
Environment variables always override defaults.
Instead of stopping at the first validation error, envfy collects every validation error before throwing.
Example:
PORT=abc
DEBUG=yesvalidateEnv({
PORT: "number",
DEBUG: "boolean",
DATABASE_URL: "string",
});Produces:
Environment validation failed
PORT must be a valid number
DEBUG must be true or false
Missing environment variable: DATABASE_URL
envfy automatically infers the returned object.
const env = validateEnv({
PORT: "number",
DEBUG: "boolean",
NODE_ENV: ["development", "production", "test"] as const,
API_KEY: {
type: "string",
optional: true,
},
HOST: {
type: "string",
default: "localhost",
},
});TypeScript infers:
{
PORT: number;
DEBUG: boolean;
NODE_ENV: "development" | "production" | "test";
API_KEY?: string;
HOST: string;
}No manual interfaces or type assertions are required.
.env
PORT=3000
DEBUG=true
NODE_ENV=development
DATABASE_URL=postgres://localhost/dbimport { validateEnv } from "@yasserfedsi/envfy";
const env = validateEnv({
PORT: "number",
DEBUG: "boolean",
NODE_ENV: ["development", "production", "test"] as const,
DATABASE_URL: "string",
});
console.log(env);Output:
{
PORT: 3000,
DEBUG: true,
NODE_ENV: "development",
DATABASE_URL: "postgres://localhost/db",
}Validates and parses environment variables.
Returns a fully typed object containing the validated values.
Throws:
EnvValidationErrorwhen one or more environment variables fail validation.Errorwhen the schema configuration itself is invalid.
- String validation
- Number validation
- Boolean validation
- Enum validation
- Optional variables
- Default values
- Aggregated validation errors
- Automatic TypeScript type inference
- Custom validators
- Value transformers
- Array support
- Custom error formatting
- Browser compatibility
Contributions, bug reports, feature requests, and pull requests are welcome.
Please read the contribution guidelines before opening a pull request.
See CHANGELOG.md for a complete release history.
MIT License.