Skip to main content
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:
  1. Development Environment: Uses .env files loaded via Node.js --env-file flag with nodemon
  2. Production Environment: Uses Docker environment variables injected at container runtime
This approach ensures that:
  • Developers can work with standard .env files during development using npm 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 the services/backend directory:

.env (Base Configuration)

.env.local (Local Overrides)

Environment File Priority

Node.js with --env-file loads environment variables in this order:
  1. System environment variables (highest priority)
  2. .env file variables
  3. Default values in code (lowest priority)
Note: Unlike some frameworks, Node.js --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 (from nodemon.json):
This configuration:
  • 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:
Runtime environment variables will override these defaults.

Using Environment Variables in Code

Accessing Variables

Environment variables are directly accessible via process.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