Skip to main content
Deployment8 min readPublished September 19, 2026Updated September 22, 2026

Environment variables explained for beginners

An environment variable is a named value supplied to your program from outside its code, so the same code can behave differently in different places. Your local machine, your preview and your live site are separate environments, each with its own set of values — which is why a variable working in one proves nothing about the others.

What problem do they solve?

Two things need to change between your laptop and your live site: secrets, which must not be written into files, and settings, which differ per environment — a test payment key locally, a real one in production.

The value is read, not written, in code
const apiKey = process.env.STRIPE_SECRET_KEY;
if (!apiKey) throw new Error("STRIPE_SECRET_KEY is not set");

The explicit check matters. Without it, an unset variable becomes `undefined`, the request goes out with no credential, and you get a confusing 401 instead of a clear message about configuration.

Which variables reach the browser?

Build tools only expose variables with a specific prefix to browser code — commonly `VITE_` or `NEXT_PUBLIC_`. Everything else stays on the server. That prefix is a decision, not a formality: prefixing a secret makes it public.

NameVisible to visitors?Suitable for
VITE_SUPABASE_URLYesPublic project URL
VITE_SUPABASE_PUBLISHABLE_KEYYesKeys designed to be public
STRIPE_SECRET_KEYNoServer-only secrets
RESEND_API_KEYNoServer-only secrets

If you ever prefix a secret to 'make it work in the component', the real fix is to move that code to the server.

Why is my variable undefined?

  1. 1Confirm it is set in the environment you are actually running — local, preview and production are separate lists.
  2. 2Check the spelling exactly, including case. `API_KEY` and `Api_Key` are different variables.
  3. 3Restart the dev server or redeploy. Most environments read variables at start-up, not per request.
  4. 4For browser-visible values, confirm the required prefix is present and that you rebuilt after adding it.
  5. 5Read the value inside the function that uses it, not at the top of the module, where it may be evaluated before the environment is ready.

Why does the build bake some values in?

Browser-visible variables are substituted into your JavaScript when the project is built. Changing one afterwards does nothing until you build again. Server-side variables are read while running, so changing one takes effect on the next restart.

This is why 'I added the variable and nothing changed' is almost always a missing rebuild for front-end values, and a missing restart for server values.

What does AI often get wrong here?

  • Reading `process.env` at the top of a module, where the value can be undefined at import time.
  • Inventing a variable name that does not match the one you actually created.
  • Assuming a variable set locally exists in production too.
  • Silently defaulting to an empty string instead of failing loudly.

Frequently asked questions

Is a .env file secure?
Only if it is never committed. Keep it out of version control, and treat any value that has been committed as leaked and in need of rotation.
Can I see my production variables?
You can see the names in your hosting configuration. Many platforms hide secret values after they are saved, by design.
Do environment variables work in a static site?
Only the browser-visible ones, and only as values baked in at build time. There is no server to keep a secret.

Practice this in MessyDev

Reading it once helps. Doing it once sticks. These are the hands-on parts of MessyDev that cover the same ground.

Keep going