home / skills / a5c-ai / babysitter / dotenv-integration
This skill helps you load and validate environment variables with dotenv, expand files, and type-coerce values for reliable configuration.
npx playbooks add skill a5c-ai/babysitter --skill dotenv-integrationReview the files below or copy the command above to add this skill to your agents.
---
name: dotenv-integration
description: Integrate dotenv for environment variable loading with validation and type coercion.
allowed-tools: Read, Write, Edit, Bash, Glob, Grep
---
# Dotenv Integration
Integrate dotenv for environment variable loading.
## Generated Patterns
```typescript
import { config } from 'dotenv';
import { expand } from 'dotenv-expand';
import { z } from 'zod';
// Load .env files in order
for (const file of ['.env.local', `.env.${process.env.NODE_ENV}`, '.env']) {
expand(config({ path: file }));
}
const envSchema = z.object({
NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
PORT: z.coerce.number().default(3000),
DATABASE_URL: z.string().url(),
API_KEY: z.string().min(1),
});
export const env = envSchema.parse(process.env);
```
## Target Processes
- configuration-management-system
- mcp-server-bootstrap
- cli-application-bootstrap
This skill integrates dotenv-based environment loading with validation and type coercion to ensure reliable configuration at runtime. It combines dotenv, dotenv-expand, and Zod to load multiple .env files in a predictable order and to validate and coerce values into typed application settings. The result is a single exported env object your application can import with confidence.
The skill iterates through a prioritized list of .env files (for example: .env.local, .env.NODE_ENV, .env) and loads them using dotenv and dotenv-expand so variables and expansions are resolved. After loading, it runs a Zod schema against process.env to coerce types (numbers, enums) and to enforce required values and formats (URLs, non-empty strings). It exports the parsed and typed env object for direct consumption by the codebase.
What happens if an environment variable is missing or invalid?
The Zod schema parse will throw an error at startup, causing a fail-fast behavior so issues are detected immediately.
How are multiple .env files merged?
Files are loaded in the specified order and later values can overwrite earlier ones; dotenv-expand resolves nested references before validation.