Type-safe environment variable validation for Node.js applications with development-time warnings.
- π Type-safe validation - Validate and transform environment variables with TypeScript support
- π‘οΈ Required/Optional flags - Mark variables as required or provide defaults
β οΈ Development warnings - Get helpful warnings during development- π― Custom validators - Add custom validation logic
- π Auto-documentation - Generate documentation for your environment schema
- π§ Flexible configuration - Customize behavior with various options
- π Clean Architecture - Well-organized, maintainable codebase
npm install env-inspectorimport { env } from 'env-inspector';
// Define your environment schema
const config = env()
.define({
PORT: {
type: 'number',
required: true,
default: 3000,
description: 'Server port number'
},
NODE_ENV: {
required: true,
validator: (value) => ['development', 'production', 'test'].includes(value)
},
DATABASE_URL: {
required: true,
description: 'PostgreSQL connection string'
},
DEBUG: {
type: 'boolean',
default: false
},
ALLOWED_ORIGINS: {
type: 'array',
default: ['http://localhost:3000']
}
})
.validate();
// Use your validated config
console.log(`Server starting on port ${config.PORT}`);
console.log(`Environment: ${config.NODE_ENV}`);
console.log(`Debug mode: ${config.DEBUG}`);The package is organized with clean separation of concerns:
src/
βββ core/
β βββ env-inspector.ts # Main EnvInspector class
βββ types/
β βββ index.ts # TypeScript interfaces and types
βββ utils/
β βββ env-loader.ts # Environment file loading
β βββ type-transformer.ts # Type transformation utilities
β βββ validator.ts # Validation logic
β βββ doc-generator.ts # Documentation generation
βββ factories/
β βββ index.ts # Convenience factory functions
βββ index.ts # Main entry point
interface EnvInspectorOptions {
envPath?: string; // Path to .env file (default: '.env')
silent?: boolean; // Suppress warnings (default: false)
throwOnMissing?: boolean; // Throw on missing required vars (default: false)
prefix?: string; // Prefix for environment variables
}interface EnvVarConfig {
required?: boolean; // Is this variable required?
default?: any; // Default value if missing
type?: 'string' | 'number' | 'boolean' | 'array'; // Type transformation
validator?: (value: string) => boolean; // Custom validation function
description?: string; // Documentation description
transform?: (value: string) => any; // Custom transformation function
}Define the environment variable schema.
Validate all defined environment variables and return the validated config.
Get a specific validated environment variable.
Get all validated environment variables.
Generate markdown documentation for the environment schema.
import { EnvInspector } from 'env-inspector';
const inspector = new EnvInspector();
const config = inspector
.define({
API_KEY: { required: true },
PORT: { type: 'number', default: 3000 }
})
.validate();const config = env()
.define({
EMAIL: {
required: true,
validator: (value) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value),
description: 'Valid email address'
},
URL: {
required: true,
validator: (value) => {
try {
new URL(value);
return true;
} catch {
return false;
}
}
}
})
.validate();const inspector = env()
.define({
DATABASE_URL: {
required: true,
description: 'PostgreSQL connection string'
},
REDIS_URL: {
required: false,
default: 'redis://localhost:6379',
description: 'Redis connection string for caching'
}
});
// Generate markdown documentation
console.log(inspector.generateDocs());// Throw on missing required variables
const config = env({ throwOnMissing: true })
.define({
SECRET_KEY: { required: true }
})
.validate(); // Will throw if SECRET_KEY is missing
// Or handle errors gracefully
const config = env({ silent: true })
.define({
SECRET_KEY: { required: true }
})
.validate(); // Will log errors but not throw# Install dependencies
npm install
# Build the project
npm run build
# Run tests
npm test
MIT
Contributions are welcome! Please feel free to submit a Pull Request.