@adonisjs/env, providing type-safe environment variable management with validation schemas.
Import
Overview
The Env module provides classes and utilities for managing environment variables with type safety and validation. It ensures that your application has all required environment variables with the correct types before startup.Classes
Env
The main environment variable management class with built-in validation.Static Methods
create
Creates a new Env instance with validation schema.The root directory URL of your application (typically
new URL('../', import.meta.url))An object defining validation rules for environment variables using
Env.schema methodsA promise that resolves to an Env instance with validated environment variables
Instance Methods
get
Retrieves a validated environment variable value.The environment variable name
The validated and type-cast environment variable value
Validation Schema
Env.schema
Provides validation methods for different types of environment variables.string
Validates a string environment variable.number
Validates a number environment variable.Optional configuration object
boolean
Validates a boolean environment variable.Optional configuration object
enum
Validates an enum environment variable with allowed values.Array of allowed values (use
as const for TypeScript literal types)Types
EnvParser
Type for the environment parser instance.EnvEditor
Type for editing environment files programmatically.Example Usage
Notes
- Environment variables are validated at application startup
- Validation failures prevent the application from starting
- Type information is preserved for better IDE autocomplete
- Environment files (
.env) are automatically loaded from the app root - The Env instance is typically available via dependency injection in your application