DevOps
How environment variables work
Environment variables are named values that live in a process and get inherited by the processes it spawns. They are the standard way to pass configuration and secrets into an app without hardcoding them.
Setting and reading them
export API_URL=https://api.example.com
echo $API_URL
printenv | sort # list everything in the current environment
Set one just for a single command by prefixing it:
NODE_ENV=production node server.js
Inheritance
A child process inherits a copy of its parent's environment. Changing a variable in the child does not affect the parent, and a variable set without export is not passed down at all.
Reading them in code
const url = process.env.API_URL;
if (!url) throw new Error('API_URL is not set');
import os
url = os.environ.get('API_URL', 'http://localhost:3000') # with a default
The .env pattern
For local development, keep values in a .env file and load them with a library like dotenv. Never commit that file.
# .env
DATABASE_URL=postgres://localhost/dev
SESSION_SECRET=change-me
Add it to .gitignore and commit a .env.example with blank or dummy values so teammates know which keys to set.
Secrets are not really hidden
Anyone who can run printenv in your process, or read your deployment config, can see them. Env vars keep secrets out of source control, not out of a compromised machine. For production, use a real secrets manager.
