Envark’s validation feature ensures your .env files contain all the environment variables your code actually uses, with proper values. It catches configuration errors before deployment by comparing your .env files against real code usage.
Variables used in code but not defined in the .env file:
// From src/tools/validate_env_file.ts:186-207for (const varName of usedInCode) { if (!envVars.has(varName)) { // Check if it has a default value in code const variable = resolved.variables.get(varName); if (variable && !variable.hasDefault) { failed.push({ variable: varName, status: 'fail', issue: 'Used in code but missing from env file', suggestion: 'Add this variable to your env file' }); } else if (variable?.hasDefault) { warnings.push({ variable: varName, status: 'warning', issue: 'Used in code but missing from env file (has default)', suggestion: 'Consider adding explicitly for clarity' }); } }}
Variables defined in .env but never referenced in code:
// From src/tools/validate_env_file.ts:141-151if (!usedInCode.has(varName)) { warnings.push({ variable: varName, status: 'warning', issue: 'Defined in env file but never used in code', suggestion: "Remove if not needed, or verify it's used indirectly", value: value.length > 20 ? value.slice(0, 20) + '...' : value }); continue;}
Example:
# .envOLD_API_KEY=abc123 # ⚠ WARNING: Not used anywhere
// From src/tools/validate_env_file.ts:154-163if (value.trim() === '') { failed.push({ variable: varName, status: 'fail', issue: 'Empty value', suggestion: 'Set a valid value or remove if not needed', value: '(empty)' }); continue;}
$ envark validate .env┌─ VALIDATION ──────────────────────────────────────────────┐│ Status: ✗ INVALID│ Passed: 35 Warnings: 3 Failed: 4└──────────────────────────────────────────────────────────┘✓ PASSED (35) DATABASE_URL postgresql://... API_KEY sk-abc... PORT 3000 ...⚠ WARNINGS (3) OLD_FEATURE_FLAG Defined but never used in code LEGACY_API_URL Defined but never used in code BACKUP_ENABLED Missing from file (has default)✗ FAILED (4) SECRET_KEY Empty value STRIPE_KEY Placeholder: "your-key-here" DATABASE_PASSWORD Missing from env file REDIS_URL Missing from env fileRECOMMENDATIONS: • Set a value for SECRET_KEY • Replace placeholder in STRIPE_KEY with actual key • Add DATABASE_PASSWORD and REDIS_URL to .env • Consider removing unused OLD_FEATURE_FLAG and LEGACY_API_URL
envark validate [env-file] [options]Options: --project <path> Project directory to scan (default: current directory) --fail-on-errors Exit with code 1 if validation fails --fail-on-warnings Exit with code 1 if warnings found --json Output results as JSON --quiet Only show summary --verbose Show detailed information
0 - Validation passed (or only warnings if --fail-on-warnings not set)1 - Validation failed (critical issues found)2 - Invalid arguments or file not found