Prompt
Use consistent patterns for environment variable handling and configuration validation. Access environment variables directly where they’re needed rather than loading them unnecessarily in build configurations. Provide clear fallback mechanisms and fail fast with descriptive error messages when required configuration is missing or invalid.
Key practices:
- Access
process.envdirectly in the consuming module instead of loading environment variables in build/config files - Use environment variables instead of hard-coded paths: prefer
ZERO_SCHEMA_PATH=shared/schema.tsover hard-coding paths - Implement proper fallback chains with validation:
const loadSchemaJson = () => {
if (process.env.ZERO_SCHEMA_JSON) {
return process.env.ZERO_SCHEMA_JSON;
}
try {
const schema = readFileSync("zero-schema.json", "utf8");
return JSON.stringify(JSON.parse(schema));
} catch (error) {
throw new Error(
"Schema must be provided via ZERO_SCHEMA_JSON env var or zero-schema.json file"
);
}
};
- Fail fast on configuration conflicts rather than issuing warnings that may be ignored
- Add new environment variables to
.env.examplefiles for documentation - Validate configuration values early in the application lifecycle with clear error messages