Skip to content

Environment Variables

Use environment variables to configure applications across different stages without code changes.

What you'll learn

  • What environment variables are and why they are used
  • How to set, read, and persist environment variables
  • Using .env files for local development
  • Best practices for managing configuration

Concept

What Are Environment Variables?

Environment variables are key-value pairs stored in the operating system's environment. Any process running on the system can read them. They are the standard way to configure applications without hardcoding values in the source code. For example, a database URL, an API port, or a log level can all be set as environment variables so the same code runs in development, staging, and production with different settings.

Setting and Reading Variables

In the shell, you set an environment variable with export KEY="value". Once exported, the variable is available to the current shell and all processes started from it. To read a variable, use $KEY in the shell or process.env.KEY in Node.js, os.environ in Python, or os.Getenv in Go. The printenv command lists all current environment variables.

.env Files

For local development, teams use .env files — plain text files containing KEY=value lines. Tools like dotenv (Node.js) and python-dotenv load these files at startup. Never commit .env files to version control — they often contain secrets. Add .env to your .gitignore and provide a .env.example template with placeholder values instead.

Configuration Best Practices

Keep configuration separate from code. Use environment variables for anything that changes between environments: database connections, API keys, feature flags, and service ports. Give variables sensible defaults so the app works out of the box in development. Validate required variables at startup so the app fails fast with a clear message instead of crashing later with a cryptic error.

Environment Hierarchy

A typical project uses different variables per environment: development uses a local database, staging uses a test database, and production uses a managed cloud database. The same code reads all of them through environment variables. Tools like Docker, Kubernetes, and cloud platforms all support environment variable injection natively. This pattern — known as the Twelve-Factor App methodology — keeps configuration strictly separate from code, making applications portable across environments without any code changes or rebuilds.

Example

              # Set environment variables in the shell
export NODE_ENV="production"
export PORT=3000
export DATABASE_URL="postgres://user:pass@localhost:5432/myapp"
export LOG_LEVEL="info"

# Read them in any process
echo "Running on port $PORT in $NODE_ENV mode"
printenv | grep -E "PORT|NODE_ENV"   # show specific variables

# A .env file for local development
cat > .env << 'EOF'
NODE_ENV=development
PORT=3000
DATABASE_URL=postgres://localhost:5432/myapp_dev
LOG_LEVEL=debug
EOF
            

Environment variables are set with export, read with $VAR, and loaded from .env files in development. The heredoc (<< 'EOF') writes multiple lines to a file.

Try it

Common mistakes

The mistake

Committing .env files with secrets to git

The fix

Add .env to .gitignore immediately. Provide a .env.example with fake values. If a secret was already committed, rotate it — assume it is compromised.

The mistake

Hardcoding configuration values in source code

The fix

Read all environment-specific values from environment variables. This lets the same code run in dev, staging, and production without code changes.

Practice Environment Variables

2 exercises

Practice Environment Variables

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors