Backend Environment Variables
The DeployStack backend uses Node.js environment variables that work seamlessly across development and production environments. This system supports both local.env
files for development and Docker environment variable injection for production deployments.
Overview
The backend environment system consists of two main approaches:- Development Environment: Uses
.env
files loaded via Node.js--env-file
flag with nodemon - Production Environment: Uses Docker environment variables injected at container runtime
- Developers can work with standard
.env
files during development usingnpm run dev
- Production deployments can inject variables at runtime without rebuilding the application
- Environment variables are directly accessible via
process.env
throughout the Node.js application - The same codebase works seamlessly in both environments
Environment Variable Types
Development Environment Variables
During development, the backend loads environment variables from.env
files using Node.js’s built-in --env-file
support:
Production Environment Variables
In production Docker containers, variables are injected at runtime via Docker’s environment variable system:Development Setup
Environment Files
Create environment files in theservices/backend
directory:
.env
(Base Configuration)
.env.local
(Local Overrides)
Environment File Priority
Node.js with--env-file
loads environment variables in this order:
- System environment variables (highest priority)
.env
file variables- Default values in code (lowest priority)
--env-file
doesn’t support multiple env files by default. Use .env.local
by renaming it to .env
for local development.
Development Example
Nodemon Configuration
The development server uses nodemon with the following configuration (fromnodemon.json
):
- Loads environment variables from
.env
using--env-file=.env
- Sets
NODE_ENV=development
by default - Watches TypeScript files for changes
- Uses ts-node for TypeScript compilation
Production Deployment
Docker Environment Variables
The production Docker container accepts environment variables at runtime:Docker Compose Example
Dockerfile Environment Handling
The production Dockerfile creates a default.env
file with basic settings:
Using Environment Variables in Code
Accessing Variables
Environment variables are directly accessible viaprocess.env
in Node.js:
Server Configuration Example
Database Configuration Example
Plugin System Configuration
Adding New Environment Variables
Step 1: Add to Environment Files
Add the new variable to your.env
file:
Step 2: Use in Code
Access the variable in your TypeScript code:Step 3: Update Production Configuration
Add the variable to your production deployment configuration:Step 4: Create Type-Safe Helpers (Optional)
For better type safety and validation:Environment Variable Naming Conventions
Backend Environment Variables
- Use
SCREAMING_SNAKE_CASE
format - Be descriptive and specific
- Group related variables with prefixes
Common Patterns
Debugging Environment Variables
Development Debugging
Production Debugging
Related Documentation
- Backend Development Guide - Main backend development guide
- Database Configuration - Database setup and configuration
- API Documentation - API development guide
- Deploy DeployStack - Deploy DeployStack with Docker Compose