Relevant source files
The following files were used as context for generating this wiki page:
This page details the configuration management within the project, focusing on how environment variables are loaded and how application-specific settings for services like AWS and "Better Auth" are defined and validated. The system leverages dotenv for loading environment variables and zod for robust schema validation, ensuring that all necessary configurations are present and correctly formatted at application startup.
The configuration architecture aims to provide a clear, centralized, and validated approach to managing application settings, reducing runtime errors caused by missing or malformed environment variables.
Environment Variable Loading
The project utilizes a dedicated module, load-env.ts, to manage the loading of environment variables from .env files. This module ensures that environment variables are loaded early in the application lifecycle, supporting various deployment and development environments. It searches for .env files in predefined locations to maximize flexibility.
The loadEnv function attempts to load the .env file from several common paths:
- Relative to the compiled output (
dist/src/.env). - Relative to the source directory (
src/.env). - In the current working directory (
process.cwd()/.env).
Once an .env file is found, its variables are loaded and can optionally override existing environment variables. The ensureEnvLoaded function provides a mechanism to guarantee that environment variables have been processed, preventing redundant loading.
// apps/api/src/config/load-env.ts
const searchPaths = [
// When compiled to dist/src (Nest build output)
path.join(__dirname, '..', '..', '.env'),
// When running with ts-node directly from src
path.join(__dirname, '..', '.env'),
// Fallback to current working directory
path.join(process.cwd(), '.env'),
];
function loadEnv(): void {
for (const envPath of searchPaths) {
if (existsSync(envPath)) {
config({ path: envPath, override: true });
envLoaded = true;
return;
}
}
envLoaded = true;
}Environment Loading Flow
Sources: apps/api/src/config/load-env.ts:1-25
Application-Specific Configurations
The project uses @nestjs/config's registerAs function in conjunction with zod schemas to define and validate specific configuration objects. This approach provides type safety and ensures that critical application settings are correctly structured and present.
AWS Configuration
The AWS configuration (aws.config.ts) defines the necessary parameters for interacting with Amazon Web Services. It includes settings for region, access keys, and S3 bucket details.
The AWS configuration is validated at application startup using a zod schema. If any required environment variables are missing or invalid, the application will throw an error and fail to start, preventing potential runtime issues.
AWS Configuration Options
| Option | Environment Variable | Description | Default Value | Required |
|---|---|---|---|---|
region | APP_AWS_REGION | AWS region to connect to. | us-east-1 | No |
accessKeyId | APP_AWS_ACCESS_KEY_ID | AWS access key ID. | Yes | |
secretAccessKey | APP_AWS_SECRET_ACCESS_KEY | AWS secret access key. | Yes | |
bucketName | APP_AWS_BUCKET_NAME | Name of the S3 bucket to use. | Yes | |
endpoint | APP_AWS_ENDPOINT | Custom endpoint for AWS services (e.g., for localstack). | No |
Sources: apps/api/src/config/aws.config.ts:1-33
AWS Configuration Schema
Better Auth Configuration
The "Better Auth" configuration (better-auth.config.ts) specifies the URL for an external authentication service.
The BETTER_AUTH_URL environment variable is required. If it is not provided or is not a valid URL, the application will fail to start.
Better Auth Configuration Options
| Option | Environment Variable | Description | Required |
|---|---|---|---|
url | BETTER_AUTH_URL | The URL of the "Better Auth" service. | Yes |
Sources: apps/api/src/config/better-auth.config.ts:1-27
Better Auth Configuration Schema
Configuration Validation Flow
General Environment Variable Setup
For local development, environment variables are typically managed through .env files. The project provides example .env.example files that should be copied and filled with actual credentials.
Create .env files
Copy the example environment files for each application and package.
cp apps/app/.env.example apps/app/.env cp apps/portal/.env.example apps/portal/.env cp packages/db/.env.example packages/db/.envFill out required variables
Populate the newly created
.envfiles with your specific credentials and settings.ImportantEnsure all required variables are present. The
README.mdspecifically highlights the need forNEXT_PUBLIC_PORTAL_URLandREVALIDATION_SECRETincomp/apps/app/.envwhich might be missing from.env.example.Common Environment Variables
| Variable | Description
On This Page
Environment Variable LoadingEnvironment Loading FlowApplication-Specific ConfigurationsAWS ConfigurationAWS Configuration OptionsAWS Configuration SchemaBetter Auth ConfigurationBetter Auth Configuration OptionsBetter Auth Configuration SchemaConfiguration Validation FlowGeneral Environment Variable SetupCreate .env filesFill out required variablesCommon Environment Variables