Nviron is a lightweight, type-safe e[nviron]ment variable management library built for modern JavaScript and TypeScript projects. It helps you define, validate, and safely access environment variables using Zod.
Explore the docs »
Report Bug
·
Request Feature
Nviron simplifies how you work with environment variables in modern JavaScript and TypeScript projects.
Instead of manually checking process.env or writing repetitive validation logic, nviron provides a declarative, type-safe, and Zod powered approach that ensures your app runs with confidence no missing or invalid envs.
To start using nviron, simply install it in your Node.js or TypeScript project. It’s lightweight, type-safe, and works out of the box with Zod
Install nviron and optionally zod (only if you need full Zod functionality outside of nviron):
pnpm add nvironnpm install nvironyarn add nvironnviron re-exports Zod as z, so for defining and validating environment variables you can do:
import { defineEnv, z } from "nviron";
const env = defineEnv({
PORT: z.coerce.number(),
DATABASE_URL: z.string().url(),
});Here’s a quick example to get started:
import { defineEnv, z } from "nviron";
// Default usage (process.env)
const env = defineEnv({
PORT: z.coerce.number(),
DATABASE_URL: z.string().url(),
NODE_ENV: z.enum(["development", "production"]),
});
console.log(env.PORT); // Safely typed & validated
// Using a custom source and prefix (e.g., Vite)
const envVite = defineEnv(
{
PORT: z.coerce.number(),
DATABASE_URL: z.string().url(),
NODE_ENV: z.enum(["development", "production"]),
},
{
source: import.meta.env, // or any custom object
prefix: "VITE_",
},
);
console.log(envVite.PORT); // Typed & validateddefineEnv(schema, config?) accepts an optional configuration object:
source?: EnvData— Environment source object (defaults toprocess.env).prefix?: string— Prefix to strip from environment variable names before validation (useful for Vite or custom setups).
⚠️ Prefixed variables (likeVITE_PORT) are automatically normalized to match your schema keys (PORT) before validation.
If any variable is missing or invalid, nviron will display a clear, color-coded error message and exit gracefully helping you catch issues early.
❌ Environment Variable Missing or Invalid
1 issue found in your environment configuration.
1. API_KEY → Invalid input: expected string, received undefined
💡 Check your .env file or environment variables before starting the server.nviron exports types to improve TypeScript experience:
EnvConfig- config object shapeEnvSchema- type for your environment schemaValidatedEnv- type of the validated environment objectz- re-exported Zod for convenience
For more examples, detailed configuration options, please refer to the Documentation
- Core
nvironpackage - Zod validation support
- Better error diagnostics & colored console output
- CLI tool
- Environment file generation (
.env.examplecreator) - Schema auto-generation from
.envfiles
- Environment file generation (
Contributions are always welcome! 🎉 We believe open source grows stronger through collaboration whether it’s bug fixes, documentation improvements, typo or brand-new features.
If you’d like to contribute, please read our detailed CONTRIBUTING.md