All academies

Simulator

Environment Variables Simulator

Edit a .env file and watch process.env update live. Switch between development and production to see how the same code reads different config.

.env

4 variables parsedLines starting with # are comments.

Reading them in Node

app.js
// Node 20+: load .env natively with node --env-file=.env app.js
// (or: require('dotenv').config() with the dotenv package)

const port = Number(process.env.PORT ?? 3000);   // fallback + coerce
const dbUrl = process.env.DB_URL;

if (!dbUrl) {
  // validate on boot — fail fast, fail loud
  throw new Error('DB_URL is required');
}

const isProd = process.env.NODE_ENV === 'production';
console.log(`Starting on :${port} (${isProd ? 'prod' : 'dev'})`);

process.envwhat your code sees

keyvalue
PATH/usr/local/bin:/usr/bin:/binsystem
HOME/home/usersystem
USERusersystem
SHELL/bin/zshsystem
PORT3000
DB_URLpostgres://localhost:5432/myapp_dev
API_KEYsecret123 secret — never log this
NODE_ENVdevelopment

Dimmed rows come from the operating system — they exist before your .env is even read. Loading .env just merges more strings into the same object.

Development mode

Local ports, a local database, throwaway API keys, verbose logging. The .env file lives on your machine only — it is convenience config, and even here it stays out of git.

Best practices checklist

  • Never commit .env — add it to .gitignore before your first commit.
  • Commit a .env.example with the keys but placeholder values, so teammates know what to configure.
  • Validate required variables on boot and crash loudly if one is missing — not at 3am when the code path finally runs.
  • In production, use your platform's secret manager (AWS Secrets Manager, Vault, Railway/Vercel env settings) instead of files on disk.
  • Remember: every process.env value is a string. PORT is "3000", not 3000 — parse and coerce explicitly.