🔧 AI Nachrichten ChatGPT showing blank screen [Fix](05.09.2026 um 19:55 Uhr)
⚠️ Malware / Trojaner / VirenSofort deinstallieren: Diese 19 Browser-Erweiterungen sind mit Malware verseucht(06.09.2026 um 08:00 Uhr)
🔧 AI Nachrichten GenAI Workflows für Social Media Content(02.09.2026 um 14:00 Uhr)
🔧 AI Nachrichten GenAI Workflows für Social Media Content(02.09.2026 um 14:00 Uhr)
🔧 AI Nachrichten ChatGPT showing blank screen [Fix](05.09.2026 um 19:55 Uhr)
⚠️ Malware / Trojaner / VirenSofort deinstallieren: Diese 19 Browser-Erweiterungen sind mit Malware verseucht(06.09.2026 um 08:00 Uhr)
🔧 AI Nachrichten GenAI Workflows für Social Media Content(02.09.2026 um 14:00 Uhr)
🔧 AI Nachrichten GenAI Workflows für Social Media Content(02.09.2026 um 14:00 Uhr)

🔧 Programmierung 🕛 kürzlich 4 Min Lesezeit
0

Managing Environment Variables in Node.js: The Complete Guide

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




Managing Environment Variables in Node.js: The Complete Guide



Stop hardcoding secrets. Here's how to do it right.






The Problem






CODE
// ❌ NEVER do this
const DB_PASSWORD = 'SuperSecret123!';
const API_KEY = 'sk-live-abc123def456';
const STRIPE_SECRET = 'sk_test_...';









Solution 1: .env Files (The Standard)






CODE
# .env file (NEVER commit this!)
DB_HOST=localhost
DB_PORT=5432
DB_NAME=myapp
DB_USER=admin
DB_PASSWORD=secret123
API_URL=https://api.example.com
API_KEY=abc123
NODE_ENV=development
PORT=3000









CODE
// Load .env (at the very top of your app, before anything else)
import 'dotenv/config';
// or: require('dotenv').config();

// Access variables
const dbHost = process.env.DB_HOST;
const port = process.env.PORT || 3000;









Solution 2: Validation with Zod






CODE
// config.js — Validate all env vars at startup
import { z } from 'zod';

const envSchema = z.object({
NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
PORT: z.coerce.number().default(3000),
DATABASE_URL: z.string().url(),
API_KEY: z.string().min(1),
JWT_SECRET: z.string().min(32),
REDIS_URL: z.string().optional(),
LOG_LEVEL: z.enum(['debug', 'info', 'warn', 'error']).default('info'),
});

export const config = envSchema.parse(process.env);

// If any required variable is missing or invalid → crashes at startup!
// This is GOOD — fail fast instead of failing randomly during runtime.









Solution 3: Multi-Environment Config






CODE
# .env.development (local dev)
DATABASE_URL=postgres://localhost:5432/myapp_dev
API_KEY=dev_key_123
LOG_LEVEL=debug

# .env.production (production)
DATABASE_URL=postgres://prod-db:5432/myapp_prod
API_KEY=prod_key_456
LOG_LEVEL=info

# .env.test (testing)
DATABASE_URL=:memory:
API_KEY=test_key_789
LOG_LEVEL=error









CODE
// Load the right .env file based on NODE_ENV
import dotenv from 'dotenv';
import path from 'path';

dotenv.config({
path: path.resolve(
process.cwd(),
`.env.${process.env.NODE_ENV || 'development'}`
),
});

// Fallback to default .env
if (!process.env.DATABASE_URL) {
dotenv.config({ path: '.env' });
}









Solution 12-Factor App Principles






CODE
1. Code and config are separate
2. Config is in environment variables
3. Different environments have different configs
4. Secrets are never in code or version control
5. All config is externalized (no hardcoded values)









Best Practices






.gitignore






CODE
# Never commit these files!
.env
.env.local
.env.*.local
*.env

# But DO commit templates:
# .env.example ✅









.env.example Template






CODE
# Copy this file to .env and fill in your values
# cp .env.example .env

# Server
PORT=3000
NODE_ENV=development

# Database
DATABASE_URL=postgresql://user:password@localhost:5432/mydb

# API
API_KEY=your_api_key_here
API_SECRET=your_api_secret_here

# Auth
JWT_SECRET=generate_a_random_string_at_least_32_chars
JWT_EXPIRES_IN=7d

# External services
STRIPE_SECRET_KEY=sk_test_xxx
SENDGRID_API_KEY=SG.xxxx
REDIS_URL=redis://localhost:6379









Type Safety for Env Vars






CODE
// types/env.d.ts
declare namespace NodeJS {
interface ProcessEnv {
readonly NODE_ENV: 'development' | 'production' | 'test';
readonly PORT: string;
readonly DATABASE_URL: string;
readonly API_KEY: string;
readonly JWT_SECRET: string;
// Optional vars
readonly REDIS_URL?: string;
readonly LOG_LEVEL?: 'debug' | 'info' | 'warn' | 'error';
}
}

// Now TypeScript will autocomplete and type-check your env vars!
process.env.DATABASE_URL; // string
process.env.JWT_SECRET; // string
process.env.UNKNOWN_VAR; // TypeScript error! 🎉









Docker + Environment Variables






CODE
# Dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY dist/ ./dist/
EXPOSE 3000
CMD ["node", "dist/index.js"]









CODE
# docker-compose.yml
version: '3.8'
services:
app:
build: .
ports:
- "${PORT:-3000}:3000"
environment:
- NODE_ENV=${NODE_ENV:-production}
- DATABASE_URL=${DATABASE_URL}
- API_KEY=${API_KEY}
- JWT_SECRET=${JWT_SECRET}
env_file:
- .env.production
depends_on:
- db
- redis

db:
image: postgres:15
environment:
POSTGRES_DB: myapp
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- pgdata:/var/lib/postgresql/data

volumes:
pgdata:









CI/CD Environment Variables






CODE
# GitHub Actions (.github/workflows/ci.yml)
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '18'
- run: npm ci
- run: npm test
env:
# Set via GitHub repo Settings → Secrets → Actions
DATABASE_URL: ${{ secrets.TEST_DATABASE_URL }}
API_KEY: ${{ secrets.TEST_API_KEY }}
JWT_SECRET: test-secret-for-ci-only









Security Checklist




  • [ ] No secrets in source code (including git history!)

  • [ ] .env files in .gitignore

  • [ ] .env.example committed (template without real values)

  • [ ] Secrets stored in vault/secrets manager (not .env in production)

  • [ ] Different keys per environment (dev ≠ prod)

  • [ ] Keys rotated regularly

  • [ ] Access logs for sensitive operations

  • [ ] No console.log of secrets (even in development)

  • [ ] Git pre-commit hook that scans for secrets

  • [ ] git-secrets or detect-secrets tool installed






Quick Reference












































Tool Use Case
dotenv Load .env files
zod Validate env vars
envalid Alternative validator
config Node.js built-in config
cross-env Set env vars cross-platform
dotenv-cli Run commands with .env loaded
vault HashiCorp secret management
AWS SM AWS Parameter Store / Secrets Manager





How do you manage environment variables? Any tools I missed?



Follow @armorbreak for more backend content.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 44%
🟡 In Evaluierung 26%
🟢 Keine Auswirkung 15%
Spannende Innovation 15%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
ChatGPT showing blank screen [Fix]
1 Quelle
Sofort deinstallieren: Diese 19 Browser-Erweiterungen sind mit Malware verseucht
1 Quelle
Mit dieser 90.000-mAh-Powerbank laden Sie Ihr Handy 20 Mal voll auf: Tiefstpreis bei Amazon
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Managing Environment Variables in Node.js: The Complete Guide

Thematisch verwandte Begriffe: Managing, Environment, Variables, Nodejs · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...