I Built a Zero-Dependency Env Validator with Full TypeScript Inference - Here's Why
说明与用途
在 Node.js 应用中,缺失或格式错误的环境变量会导致运行时错误。envault 在启动时验证、转换并完整类型化变量,聚合错误,屏蔽秘密,并自动生成 .env.example。
问题
Node.js applications often fail silently when required environment variables are missing or malformed, leading to runtime errors that surface only after the first request.
结果
The application starts only if all required environment variables are present and valid. Errors are reported in a single, clear message, secrets are never logged, and developers have a single source of truth for env variables that also generates an accurate .env.example.
工作流
- Install envault via npm or pnpm.
- Create a dedicated env.ts file that imports check from envault and defines a schema for all variables.
- Use the exported env object anywhere in the codebase; TypeScript infers correct types.
- Run the application; envault validates on startup, throws aggregated errors if any variable fails.
- Optionally, generate a .env.example file by calling generateExample with the schema.
- Override error handling or provide a custom env source for tests.
- Use requiredIn, validate, and array types for advanced validation scenarios.
证据
envault validates, coerces, and types your environment variables at startup, and reports every failing variable at once, not one at a time.
If anything fails, the app throws immediately with every broken variable listed at once:
Secret masking: Sensitive variables marked with secret: true never appear in error output, not even when validation fails.
- 记录日期: 2026-09-05T14:03:24.851Z