Skip to the workspace
Pebellslearn by building
Deploying the Full App  ›  Lesson

Config Lives in the Environment

Linux Basics 13 minThe same code runs everywhere and reads its settings and secrets from the process's environment variables, which come from an ignored .env file locally and a protected file on the server, while anything built into the client is public.

full stack / Config Lives in the Environment

Explore each scene at your own pace.

01 · The rule

The rule

Config Lives in the Environment

On your laptop, Sprout talks to a local database with a throwaway password. On the live server it must use the real database and a real secret. The code has to be identical in both places, so the difference must come from outside the code. On Linux, that outside is the process's environment.

The rule

Same code everywhere; the environment says where it is running.

Every Linux process carries a set of named text values, its environment variables, inherited from whatever started it. Node exposes them as process.env:

const port = Number(process.env.PORT ?? 3000);
const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });

const secret = process.env.JWT_SECRET;
if (!secret) throw new Error('JWT_SECRET is not set');

Reading config this way lets one build run anywhere. Failing loudly at startup when a required value is missing beats failing mysteriously on the first login.

In a Linux shell, export puts a variable into the shell's environment, and every program started from that shell afterwards inherits it:

export DATABASE_URL="postgres://sprout:devpass@localhost:5432/sprout"
printenv DATABASE_URL
node src/index.js

Locally, typing exports gets old, so the values live in a .env file loaded at startup, with node --env-file=.env src/index.js (Node 20.6 and later) or the dotenv package. .env is listed in .gitignore. Instead, commit a .env.example with the variable names and fake values, so a new teammate knows what to fill in.

On the server, the service manager starts the app and hands it the variables, for example from a systemd line such as EnvironmentFile=/etc/sprout/api.env. That file is readable only by its owner (chmod 600) and never enters git.

In the client, be careful. Vite lets client code use variables whose names start with VITE_, and at build time it writes their values straight into the JavaScript bundle, which anyone can read. Client variables are for public values only, such as a support email address. Secrets exist only in the server's environment.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.