env-schema-gen
v1.0.0
Published
Validate and enforce environment variable types at startup for Node.js apps. Automatically infer env types, fail fast on misconfiguration, and prevent runtime crashes.
Maintainers
Readme
🌱 env-schema-gen
Automatically validate environment variables at startup — no boilerplate, no configs.
env-schema-gen helps you catch broken or misconfigured environment variables before your app runs, by inferring types and validating them instantly.
✨ Why env-schema-gen?
In Node.js:
process.envvalues are always strings- Missing or wrong env values cause runtime crashes
- Writing manual env validation is repetitive and boring
👉 env-schema-gen acts as a startup guard for your application.
If env variables are wrong → your app will not start.
📦 Installation
npm install env-schema-gen🚀 Quick Start (30 seconds)
require("dotenv").config();
const { generateEnvSchema } = require("env-schema-gen");
// Validate envs before app starts
generateEnvSchema(process.env);
console.log("Environment is valid ✅");That’s it.
🧠 How it works
env-schema-gen:
Reads process.env
Infers types (string, number, boolean)
Validates values at startup
Throws clear errors if something is wrong
🛠 Type Inference Examples ENV Value Inferred Type 3000 number true boolean false boolean hello string ❌ Example Error (Fail Fast)
.env
PORT=abc
generateEnvSchema(process.env, { PORT: "number" });
Output:
ENV validation error: PORT expected number, got string ("abc")
Your app stops immediately — no silent failures.
🎯 Using Type Overrides (Recommended)
Inference works for most cases, but you can enforce strict types:
generateEnvSchema(process.env, { PORT: "number", DEBUG: "boolean", });
This guarantees:
PORT is always a number
DEBUG is always boolean
✅ Supported Types
string
number
boolean
📌 Real World Example (Express) require("dotenv").config(); const express = require("express"); const { generateEnvSchema } = require("env-schema-gen");
generateEnvSchema(process.env, { PORT: "number", });
const app = express(); app.listen(process.env.PORT, () => { console.log("Server running 🚀"); });
💡 When should I use this?
Use env-schema-gen if you:
Deploy Node.js apps
Use .env files
Want early failure instead of runtime bugs
Don’t want heavy validation libraries
🧩 What this package is NOT
❌ Not a full config framework
❌ Not a schema definition DSL
❌ Not tied to TypeScript
It’s intentionally small, fast, and simple.
🔮 Roadmap (Future Versions)
Required vs optional variables
Default values
.env file parsing
Zod / Joi schema output
CLI: npx env-schema-gen
🪪 License
MIT © Shubham Tidke
⭐ Like it?
If this package helped you:
⭐ Star it on GitHub
🧠 Use it in your projects
🚀 Share it with others
