Claude-skill-registry env-var-manager
Manages environment variable additions and updates across all project files. Use when adding new environment variables, updating PORT configuration, modifying deployment configurations, or documenting configuration requirements.
git clone https://github.com/majiayu000/claude-skill-registry
T=$(mktemp -d) && git clone --depth=1 https://github.com/majiayu000/claude-skill-registry "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/data/env-var-manager" ~/.claude/skills/majiayu000-claude-skill-registry-env-var-manager && rm -rf "$T"
skills/data/env-var-manager/SKILL.mdEnvironment Variable Manager
This skill ensures environment variables are properly added, documented, and configured across all project files and deployment platforms.
Files to Update When Adding Environment Variables
When adding a new environment variable, the following files must be updated in this order:
1. .env.example
(Required)
.env.exampleAdd the variable with a descriptive comment explaining its purpose and expected format.
Format:
# Description of what this variable does # Options: value1, value2, value3 (if applicable) # Default: default_value (if applicable) VARIABLE_NAME=example_value
Example:
# Server port - useful for local development or platform-specific configs # Default: 3000 PORT=3000 # Node environment - affects Express server optimization # Options: development, production, test # Default: (unset) NODE_ENV=production
2. server.js
(Required)
server.jsImplement the variable usage in the application code.
Pattern:
// Read from environment with fallback default const variableName = process.env.VARIABLE_NAME || 'default_value'; // For required variables (no default) const requiredVar = process.env.REQUIRED_VAR; if (!requiredVar) { console.error('ERROR: REQUIRED_VAR environment variable is not set'); process.exit(1); }
Examples:
// Optional with default const port = process.env.PORT || 3000; // Optional with default const nodeEnv = process.env.NODE_ENV || 'development'; // Required variable const apiKey = process.env.API_KEY; if (!apiKey) { console.error('ERROR: API_KEY environment variable must be set'); process.exit(1); }
3. docs/ENVIRONMENT-VARIABLES.md
(Required)
docs/ENVIRONMENT-VARIABLES.mdAdd comprehensive documentation in the "Currently Supported Variables" section.
Format:
### VARIABLE_NAME **Purpose**: Brief description of what this variable controls **Required**: Yes/No **Default**: `default_value` (or "None - must be set" for required vars) **Valid Values**: - `value1`: Description - `value2`: Description **Example**: ```bash VARIABLE_NAME=example_value
Platform Notes:
- AWS Elastic Beanstalk: [specific notes if applicable]
- Google Cloud Run: [specific notes if applicable]
- Docker: [specific notes if applicable]
### 4. `CLAUDE.md` (Required) Update the "Environment Variables" section to include the new variable in the list. **Format**: ```markdown **Currently Supported Variables:** - `PORT` (optional, defaults to 3000) - Server port. Useful for local development or platform-specific configurations - `NODE_ENV` (optional) - Node environment: `development`, `production`, or `test` - `NEW_VARIABLE` (required/optional, defaults to X) - Brief description
5. docker-compose.yml
(If Applicable)
docker-compose.ymlAdd to the environment section if the variable should be set in Docker containers.
Format:
services: usa-spending-app: environment: - NODE_ENV=production - PORT=3000 - NEW_VARIABLE=value
6. Dockerfile
(If Applicable)
DockerfileAdd ENV directive if the variable needs a build-time or runtime default.
Format:
# For build-time variables ARG VARIABLE_NAME=default_value # For runtime variables with defaults ENV VARIABLE_NAME=default_value
Note: Only add to Dockerfile if a default is truly needed. Prefer runtime configuration via docker-compose.yml or deployment platform.
7. Deployment Documentation (If Platform-Specific)
Update
docs/DEPLOYMENT.md if the variable requires special handling on specific platforms.
Complete Checklist for Adding Environment Variables
Use this checklist to ensure all steps are completed:
- Add to
with descriptive comment and example value.env.example - Implement in
with appropriate default or required checkserver.js - Document in
with full detailsdocs/ENVIRONMENT-VARIABLES.md - Update variable list in
"Environment Variables" sectionCLAUDE.md - Add to
environment section (if applicable)docker-compose.yml - Add to
ENV/ARG (if applicable)Dockerfile - Update
with platform-specific instructions (if needed)docs/DEPLOYMENT.md - Test locally without
file (verify defaults work).env - Test locally with
file (verify variable is read correctly).env - Verify
is in.env
(should already be there).gitignore - Never commit actual
file to repository.env - Update
under [Unreleased] > Added sectionCHANGELOG.md
Security Rules
Sensitive Values
API Keys, Passwords, Secrets, Tokens:
- NEVER hardcode in source code
- NEVER commit to
with real values (use placeholders).env.example - MUST use platform-specific secret managers in production:
- AWS: AWS Secrets Manager or Parameter Store
- Google Cloud: Secret Manager
- Heroku: Config Vars (encrypted at rest)
- Docker: Docker Secrets or external secret management
Non-Sensitive Values
Port numbers, feature flags, public URLs:
- Safe to include in
.env.example - Can have defaults in source code
Required vs Optional Variables
Required Variables (no safe default):
const apiKey = process.env.API_KEY; if (!apiKey) { console.error('ERROR: API_KEY is required but not set'); process.exit(1); }
Optional Variables (with safe defaults):
const port = process.env.PORT || 3000; const logLevel = process.env.LOG_LEVEL || 'info';
Platform-Specific Considerations
Google Cloud Run
- PORT: Automatically assigned by Cloud Run (typically 8080). DO NOT hardcode. Application must read from
.process.env.PORT - Secrets: Use Secret Manager, reference in
withgcloud run deploy--set-secrets
AWS Elastic Beanstalk
- PORT: Can be set via environment properties, but application must read from
process.env.PORT - Secrets: Use AWS Secrets Manager, reference in
or set via console.ebextensions
AWS ECS/Fargate
- Environment Variables: Set in task definition under
containerDefinitions[].environment - Secrets: Use
field in task definition to pull from Secrets Manager or Parameter Storesecrets
Heroku
- PORT: Automatically assigned by Heroku. Application must read from
process.env.PORT - Config Vars: Set via
or dashboardheroku config:set VARIABLE=value
Docker / Docker Compose
- Environment Variables: Set in
underdocker-compose.yml
or viaenvironment
file.env - Secrets: Use Docker Secrets for swarm mode, or mount secret files as volumes
Common Patterns
Feature Flags
const featureEnabled = process.env.FEATURE_NAME === 'true'; if (featureEnabled) { // Feature-specific code }
API Configuration
const apiBaseUrl = process.env.API_BASE_URL || 'https://api.example.com'; const apiTimeout = parseInt(process.env.API_TIMEOUT || '5000', 10);
Database Configuration
const dbConfig = { host: process.env.DB_HOST || 'localhost', port: parseInt(process.env.DB_PORT || '5432', 10), database: process.env.DB_NAME || 'myapp', user: process.env.DB_USER, password: process.env.DB_PASSWORD }; // Validate required fields if (!dbConfig.user || !dbConfig.password) { console.error('ERROR: DB_USER and DB_PASSWORD must be set'); process.exit(1); }
Multi-Environment Configuration
const isDevelopment = process.env.NODE_ENV === 'development'; const isProduction = process.env.NODE_ENV === 'production'; const isTest = process.env.NODE_ENV === 'test'; if (isDevelopment) { // Development-specific settings }
Testing Environment Variables
Local Testing Without .env
# Test that defaults work npm start # Verify application starts successfully # Check that default values are used
Local Testing With .env
# Create .env file cp .env.example .env # Edit .env with test values # Run application npm start # Verify variables are read correctly
Testing Required Variables
# Test that application fails gracefully when required var is missing # Remove required variable from .env npm start # Should see error message and exit with code 1
Documentation Standards
.env.example Format
# ============================================ # Server Configuration # ============================================ # Server port # Default: 3000 PORT=3000 # ============================================ # Application Settings # ============================================ # Node environment (development, production, test) # Default: (unset) NODE_ENV=production # ============================================ # API Configuration # ============================================ # External API base URL # Required: Yes API_BASE_URL=https://api.example.com # API timeout in milliseconds # Default: 5000 API_TIMEOUT=5000
ENVIRONMENT-VARIABLES.md Format
Follow the existing structure in
docs/ENVIRONMENT-VARIABLES.md:
- Currently Supported Variables (list with descriptions)
- Local Development Setup (how to set variables)
- Platform-Specific Setup (AWS, GCP, Heroku, Docker)
- Security Best Practices
- Troubleshooting
Troubleshooting
Variable Not Being Read
- Check spelling in
file matches code exactly.env - Verify
file is in project root (same directory as.env
)server.js - Restart server after changing
file.env - Check for syntax errors in
(no quotes needed, no spaces around.env
)= - Verify
package is installed if using:dotenvnpm install dotenv
Variable Not Available in Docker
- Check variable is defined in
environment sectiondocker-compose.yml - Rebuild container:
docker compose up --build - Check variable in running container:
docker exec container_name env | grep VARIABLE
Wrong Default Value Used
- Verify fallback logic:
process.env.VAR || 'default' - Check for empty string:
(empty string is falsy)process.env.VAR || 'default' - For numbers, use:
parseInt(process.env.VAR || '100', 10)
Current Environment Variables
Reference: See
docs/ENVIRONMENT-VARIABLES.md for the authoritative list.
Currently Configured:
- Server port (default: 3000)PORT
- Node environment (default: unset)NODE_ENV
Best Practices Summary
- Always update
- Never commit actual.env.example.env - Document everything - Update docs/ENVIRONMENT-VARIABLES.md with full details
- Use descriptive names -
notAPI_TIMEOUT
,TIMEOUT
notDB_HOSTHOST - Provide safe defaults - Unless the variable is truly required
- Validate required variables - Exit with clear error message if missing
- Never hardcode secrets - Use environment variables and secret managers
- Test both scenarios - With and without
file.env - Keep documentation in sync - Update all files when adding variables
- Use platform secrets - Don't put sensitive data in environment variables in production
- Follow naming conventions - UPPER_CASE_WITH_UNDERSCORES
Quick Reference Commands
# Create .env from example cp .env.example .env # View current environment variables (local) printenv | grep -E '(PORT|NODE_ENV)' # Set variable temporarily (bash/zsh) export VARIABLE_NAME=value npm start # Set variable inline (bash/zsh) VARIABLE_NAME=value npm start # Docker Compose with custom env file docker compose --env-file .env.production up # Check variable in Docker container docker exec usa-spending-search env | grep VARIABLE_NAME # AWS EB set variable eb setenv VARIABLE_NAME=value # Heroku set variable heroku config:set VARIABLE_NAME=value # GCP Cloud Run set variable gcloud run services update SERVICE_NAME --set-env-vars="VARIABLE_NAME=value"
Resources
- Project Documentation:
docs/ENVIRONMENT-VARIABLES.md - Deployment Guide:
docs/DEPLOYMENT.md - Quick Start Guide:
docs/quick-start-guide.md - Example Template:
.env.example