Frontend Environment Variables
The DeployStack frontend uses a sophisticated environment variable system that seamlessly works across development and production environments. This system supports both Vite’s build-time variables and Docker runtime variables for maximum flexibility.Overview
The frontend environment system consists of two layers:- Development Environment: Uses Vite’s built-in environment variable support with
.env
files - Production Environment: Uses Docker runtime variables that are injected at container startup
- Developers can work with standard
.env
files during development - Production deployments can inject variables at runtime without rebuilding the application
- The same codebase works seamlessly in both environments
Environment Variable Types
Vite Environment Variables (Development)
During development, Vite processes environment variables that start withVITE_
prefix:
Runtime Environment Variables (Production)
In production Docker containers, variables are injected at runtime via theenv-config.sh
script:
Current Environment Variables
Core Application Variables
Variable | Description | Default Value | Required |
---|---|---|---|
VITE_DEPLOYSTACK_BACKEND_URL | Backend API base URL | http://localhost:3000 | Yes |
VITE_APP_TITLE | Application title displayed in UI | DeployStack | Yes |
Development Setup
Environment Files
Create environment files in theservices/frontend
directory:
.env
(Base Configuration)
.env.local
(Local Overrides)
Environment File Priority
Vite loads environment files in this order (higher priority overrides lower):.env.local
(highest priority, gitignored).env.development.local
.env.development
.env
(lowest priority)
Development Example
Production Deployment
Docker Environment Variables
The production Docker container uses theenv-config.sh
script to generate runtime environment variables:
Docker Compose Example
Using Environment Variables in Code
Accessing Variables
Use the utility functions from@/utils/env
for consistent access:
Component Usage Example
Service Layer Example
Adding New Environment Variables
Step 1: Update TypeScript Definitions
Add the new variable toenv.d.ts
:
Step 2: Add to Environment Files
Add to your.env
file:
Step 3: Update Docker Configuration (if needed)
For non-VITE_ prefixed variables, updateenv-config.sh
:
Step 4: Use in Code
Environment Variable Naming Conventions
Development (Vite) Variables
- Must start with
VITE_
prefix to be accessible in the browser - Use
SCREAMING_SNAKE_CASE
format - Be descriptive and specific
Production Runtime Variables
- Can include both
VITE_
prefixed and custom variables VITE_
variables are automatically processed- Custom variables need to be explicitly added to
env-config.sh
Best Practices
Security
- Never expose sensitive data in environment variables accessible to the browser
- Use backend proxy for sensitive API keys and secrets
- Validate environment variables before using them
Performance
- Cache environment variables instead of calling
getEnv()
repeatedly - Use computed properties in Vue components for reactive environment values
Development
- Use
.env.local
for personal development settings - Document all environment variables in this guide
- Provide sensible defaults in
.env
file - Test both development and production environment setups
Troubleshooting
Common Issues
Variable Not Accessible in Browser
Problem: Environment variable is undefined in the browser Solution: Ensure the variable name starts withVITE_
Variable Not Updated in Production
Problem: Environment variable changes don’t reflect in production Solutions:- Restart the Docker container
- Check if the variable is properly passed to the container
- Verify
env-config.sh
processes the variable correctly
TypeScript Errors
Problem: TypeScript complains about unknown environment variables Solution: Updateenv.d.ts
with the new variable definition
Development vs Production Differences
Problem: Different behavior between development and production Solutions:- Use the same variable names in both environments
- Test with Docker locally:
docker build -t test . && docker run -e VITE_VAR=value test
- Use
getAllEnv()
to debug variable values
Validation Utility
Create a validation utility for critical environment variables:Related Documentation
- Frontend Development Guide - Main frontend development guide
- Deploy DeployStack - Deploy DeployStack with Docker Compose