Getting Started
Environment Manager is a type-safe, schema-driven environment variable management library for Node.js. It helps you define, validate, and access environment variables with confidence, using a simple schema API.
Features
- Dependency free and small size library for env management.
- Type-safe environment variable access
- Schema-based validation (string, number, boolean, enum, array)
- Optional and default values
- Automatic parsing from
.env
files - Direct property access for environment variables
Installation
yarn add envitron
# or
npm install --save envitron
Basic Usage
import { createEnvSchema } from 'envitron';
const env = createEnvSchema((schema) => ({
PORT: schema.number(),
NODE_ENV: schema.enum(['development', 'production', 'test'] as const),
DEBUG: schema.boolean({ optional: true }),
ALLOWED_HOSTS: schema.array({ optional: true }),
}));
// Access variables using getter method, it gives the possibility to specify a default value if none provided
const port = env.get('PORT');
const nodeEnv = env.get('NODE_ENV', 'development');
const debug = env.get('DEBUG', false);
const allowedHosts = env.get('ALLOWED_HOSTS', ['localhost']);
// Or access variables directly as properties
console.log(env.PORT); // number
console.log(env.NODE_ENV); // 'development' | 'production' | 'test'
console.log(env.DEBUG); // boolean | undefined
console.log(env.ALLOWED_HOSTS); // string[] | undefined