Environment variables and secrets across layers

Intermediate · 15 min

Configure Taskboard's API and client from the environment, and see why a prefixed Vite variable ships to every visitor while a server secret never should.

Before thisA local environment with Docker Compose

Code Step By Step · 15 min

Environment variables and secrets across layers

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Code Step By StepReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Configuration that changes between environments belongs in environment variables, read once at startup, not in code.
  • Anything in the client bundle is public, so a client-side variable is configuration, never a secret.
  • Vite exposes only variables whose names start with VITE_ to browser code, and anything with that prefix ends up in the built files.
  • Validate the environment with a schema at startup so a missing or malformed value fails immediately with a clear message.
  • Commit a .env.example with names and fake values, keep real .env files out of git, and load production secrets from the platform.

One app, many places to run

Taskboard runs in at least four places: your laptop, your teammate’s laptop, a test run in CI, and production. The database address differs in each. The session secret differs. The API’s public URL differs, and the client has to know it.

If those values sit in the code, you change code to change where it runs, and one day a production password lands in a commit. The twelve-factor idea is to keep configuration in the environment: the same code, with different values supplied from outside. The Node roadmap covers configuration and environment variables and the backend roadmap covers secrets and sensitive data handling. This lesson is about the part neither has room for: what happens when one repo has a client and a server.

The rule that matters most

Code that runs in the browser is public.

A browser app is downloaded. The visitor has every file. They can read the JavaScript, search it, and read the network tab. A value you put in the client bundle is published to everyone who opens the page, no matter how it is hidden. So client-side environment variables are configuration, not secrets. An API base URL is fine. A payment provider’s publishable key is designed to be public. A database password or a secret API key must never be there.

The server is the opposite: it runs where visitors can’t read it, so secrets live only there.

What Vite does with the prefix

Vite, the build tool Taskboard’s client uses, enforces this split with a naming rule. Only variables whose names start with VITE_ are exposed to browser code, through import.meta.env. The point is to stop you leaking by accident. I tested it with a .env file and a one-line client:

VITE_API_URL=http://localhost:3000
DATABASE_URL=postgres://taskboard:secret@localhost:5432/taskboard
STRIPE_SECRET=sk_test_not_real
VITE_OOPS_STRIPE_SECRET=sk_test_not_real_prefixed
console.log(import.meta.env.VITE_API_URL, import.meta.env.DATABASE_URL,
            import.meta.env.STRIPE_SECRET, import.meta.env.VITE_OOPS_STRIPE_SECRET);

After vite build (Vite 8.3.8), the built JavaScript contained this call:

console.log(`http://localhost:3000`, void 0, void 0, `sk_test_not_real_prefixed`)

Read it closely. The two unprefixed variables became undefined, so Vite protected them. The prefixed one was written into the bundle as plain text, even though its name says “secret”. The prefix is not a safety feature on its own. It is a declaration that you intend this value to be public, and Vite believes you. Review any VITE_ variable the way you would review text you are about to print on a billboard.

Also note: these values are baked in at build time. The built files contain the literal string. Changing the variable on the server later does not change an already-built client. Deploy the client with the right values, or have it fetch its configuration from the API at startup. That matters in environments, domains and TLS.

Validate at startup

On the server, reading process.env.PORT returns a string or undefined and nothing else. Typos pass silently. A forgotten variable shows up as a confusing error three minutes after boot. Fix it by parsing the environment once with a schema (the same Zod used elsewhere in this roadmap):

import { z } from "zod";

const Env = z.object({
  DATABASE_URL: z.url(),
  SESSION_SECRET: z.string().min(32),
  PORT: z.coerce.number().int().default(3000),
  S3_ENDPOINT: z.url().optional(),
});

export const env = Env.parse(process.env);

I ran this with Node 26.13.1. With the two required variables set, PORT came out as the number 3000, because z.coerce.number() converts the string and the default fills the gap. With nothing set, the process died immediately with a ZodError naming DATABASE_URL:

path: ["DATABASE_URL"]
message: "Invalid input: expected string, received undefined"

Failing on boot is the point. A server that cannot reach its database should never start taking traffic. Every other file imports env and gets typed, already-validated values instead of reading process.env directly.

Loading values in development

Node can read a file itself, with no extra package:

node --env-file=.env server.ts

I checked that a PORT=4000 line in a file reaches process.env.PORT. Your hosting platform will inject real variables in production, so the file is a development convenience only.

The files you keep:

File Committed? Holds
.env.example Yes Every variable name, with fake or safe values
.env No (in .gitignore) Your real local values
Platform settings n/a Production values

.env.example is the quiet hero. A new teammate copies it, fills in a few lines, and runs. If you add a variable and forget the example, the next person finds out the hard way.

Which layer reads what

For Taskboard, written down:

Variable Read by Public?
DATABASE_URL API No
SESSION_SECRET API No
S3_* credentials API No
PORT API Harmless
VITE_API_URL Client, at build time Yes, by design

The client never sees DATABASE_URL. When the client needs something the API owns, such as permission to upload a file, the API issues a short-lived, narrow credential for it. That pattern is the subject of the uploads lesson later in this group.

Rotating and leaking

Assume a secret will leak eventually: in a log line, a screenshot, a commit. Two habits cost little: keep different values per environment, so a leaked dev secret exposes nothing real, and make rotating a secret a normal operation, not an emergency. If a real secret reaches git, deleting the commit isn’t enough. Treat it as public and replace it.

What the video covers

Code Step By Step’s “React System Design #21 | Environment Variables Explained” is about fifteen minutes and was published in April 2026. Its description says it covers environment-based configuration in a React app built with Vite across development, staging and production. That fits the client half of this lesson, and I could not read a transcript to see whether it also covers the server side or the build-time baking behaviour I tested above. Syntax’s 47-minute “Stop putting secrets in .env”, published in March 2026, is linked as a second view. Its description says it discusses Varlock, a newer approach that adds schemas and validation to .env files, so it is about one specific tool and a longer listen.

Where this fits

The app can now find its database and keep its secrets out of the browser. The database is empty, and an empty app is hard to build screens against. Next: seed data and the dev database workflow.

Check yourself

  1. 01Why can a browser app never hold a real secret?

    Everything the browser runs is downloaded to the visitor, so anyone can read it in the built files or the network tab. A secret must stay on the server.

  2. 02A teammate names a variable VITE_STRIPE_SECRET. What happens?

    Vite includes it in the browser bundle because of the prefix, so the secret is published to every visitor. Secrets must not use the prefix.

  3. 03What does validating the environment at startup buy you?

    A missing or malformed value stops the server at boot with a clear error, instead of failing later in the middle of a request.

  4. 04What belongs in .env.example?

    Every variable name the app reads, with a safe placeholder value and a note, so a new teammate can copy it to .env and fill in the blanks.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark