Environment variables in production: why they are undefined after deploy
Why an environment variable that works locally is undefined after deploy: build-time values in Vite (VITE_) and Next.js (NEXT_PUBLIC_), pages rendered at build, and server processes that never got the value.
TL;DR
- Variables the browser reads (
VITE_in Vite,NEXT_PUBLIC_in Next.js (opens in a new tab)) are copied into the JavaScript when you build, so they must be set when the build runs and must never hold a secret; setting them on the server afterwards changes nothing. - In Docker (opens in a new tab), that means a build argument (
ARGand--build-arg), notdocker run -e, and a.envfile that is gitignored or in.dockerignorenever reaches the build. - A Next.js page rendered at build time freezes server-side values too; read them during a request (for example after
await connection()). - Server-only variables must be given to the running process (
docker run --env-file,env_filein Compose, or your host's settings), and the app should exit with a clear message when one is missing.
On this page
When environment variables are undefined after deploy, the value was almost always needed at a different moment than when it was set. Frontend values (VITE_ in Vite, NEXT_PUBLIC_ in Next.js) are copied into the JavaScript while the app is built, so they must exist when the build runs; server values are read while the app runs, so the running process must be given them. A .env file on your laptop serves both during development, which hides the difference until the first deploy. This is one item of the production checklist for a vibe-coded app.
Tested on 11 November 2026, with a fresh Vite 8.3.8 app from npm create vite@latest (React and TypeScript), a fresh Next.js 16.4.6 app from create-next-app, and a small Node.js (opens in a new tab) server, all on Node.js 24 in Docker.
Two kinds of variables
| Build-time (public) | Run-time (server) | |
|---|---|---|
| Examples | VITE_API_URL, NEXT_PUBLIC_SUPABASE_URL | DATABASE_URL, STRIPE_SECRET_KEY, SESSION_SECRET |
| Read by | The build, which writes the value into the JavaScript | The server process, each time it reads process.env |
| Must be set | Where npm run build runs: your CI job or the Docker build | Where the app runs: the container or the host |
| Who can see it | Every visitor, in the downloaded JavaScript | Only the server |
| Changing it needs | A new build | A restart with the new environment |
Most "undefined" bugs are a value in the wrong column: a public value set only on the server, or a server value that lived in a .env file that never left your computer.
Vite: import.meta.env and the VITE_ prefix
Vite exposes variables to your code under import.meta.env, and only those whose names start with VITE_. Its docs say they are "statically replaced at build time". The test app printed two of them:
<p>VITE_API_URL = {String(import.meta.env.VITE_API_URL)}</p>
<p>API_SECRET = {String(import.meta.env.API_SECRET)}</p>It was built in a Docker image (npm run build, then the dist/ folder served by Caddy (opens in a new tab)) four ways:
| How the value was given | What the built JavaScript contained |
|---|---|
| Not at all | VITE_API_URL = undefined |
Only at run time, docker run -e VITE_API_URL=https://api.example.com | still undefined |
At build time, --build-arg VITE_API_URL=https://api.example.com | https://api.example.com |
In a .env file in the project folder | the value, until .env was added to .dockerignore; then undefined |
API_SECRET was undefined every time, even when passed to the build: without the prefix, Vite does not expose it to client code.
The built files are static: once Vite wrote undefined into them, nothing on the server can change it. A .env file works locally, but it is gitignored, so the CI checkout or the Docker build never has it.
A Dockerfile that passes the value to the build
Declare the variable as a build argument in the stage that builds, before the build command:
FROM node:24-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
ARG VITE_API_URL
RUN npm run build
FROM caddy:2
COPY --from=build /app/dist /srv
COPY Caddyfile /etc/caddy/Caddyfiledocker build --build-arg VITE_API_URL=https://api.example.com -t my-frontend .An ARG is visible to the RUN steps after it as an environment variable, which is where Vite reads it. Keep .env in .dockerignore all the same: COPY . . would otherwise copy it, with any secrets in it, into the build stage, and into the image itself if the Dockerfile has only one stage. In Docker Compose, the same goes under build: args:. In a CI pipeline that builds, set the variables in the build job's environment.
To check a build, search its output for the value: grep -r "api.example.com" dist/. No match means the build did not have it.
Next.js: NEXT_PUBLIC_ and pages rendered at build
Next.js follows the same rule for the browser. Its docs: NEXT_PUBLIC_ values are inlined "at build time", and "after being built, your app will no longer respond to changes to these environment variables". In the test app's browser bundle, a client component's process.env.NEXT_PUBLIC_API_URL stayed a reference to an unset variable when the build had no value (so the browser shows undefined), and became the literal "https://api.example.com" when the build got --build-arg NEXT_PUBLIC_API_URL=…. Lookups by a computed name, such as process.env[name], are never inlined.
Next.js adds a second trap: server code can run at build time too. The starter's home page is static, so next build rendered it once, during the build. The test image was built with no variables and started with both set:
docker run -e SERVER_VALUE=set-at-runtime -e NEXT_PUBLIC_API_URL=https://api.example.com -p 3000:3000 my-next-app| Page | What it showed |
|---|---|
/, static, server component reading process.env.SERVER_VALUE | undefined: the HTML was made during the build |
/, client component reading NEXT_PUBLIC_API_URL | undefined |
/dynamic, reading SERVER_VALUE after await connection() | set-at-runtime |
next build marks each route in its output: in the test, / was ○ (Static), "prerendered as static content", and /dynamic was ◐ (Partial Prerender), a static shell with its dynamic part rendered on each request.
To read a server value per request, make that part dynamic. The Next.js docs use connection():
import { connection } from "next/server";
import { Suspense } from "react";
async function ServerValue() {
await connection();
return <p>{process.env.SERVER_VALUE}</p>;
}
export default function Page() {
return (
<Suspense fallback={<p>Loading</p>}>
<ServerValue />
</Suspense>
);
}The Suspense wrapper is needed in this starter: create-next-app 16.4.6 turns on cacheComponents by default, and without it next build stopped with Route "/dynamic": Next.js encountered uncached or runtime data during prerendering.
One image for several environments
Because public values are baked in, an image built for staging carries staging's API address into production. The options are to build once per environment, each with its own build arguments, or, as Next.js's docs suggest for images promoted between environments, to keep only truly fixed values public and let the browser get the rest from your own API.
Server code: the process must get the value
For code that runs on the server (an Express or Fastify API, Next.js route handlers, server actions), process.env is read while the app runs. Undefined there means the process was started without the variable. Common reasons:
- The variables were in
.env, which is (rightly) not in Git and not in the image, and nothing else provides them on the server. - The app loads
.envwith a library only in development. - The container was started without
-e,--env-fileor Compose'senv_file. - The variable is set in the host's shell but not passed into the container.
Give the container its environment from a file that lives on the server:
services:
api:
image: my-api
env_file: app.env
environment:
PORT: "3000"env_file loads every line of app.env; environment sets single values. In the test, both reached the process. Keep app.env out of Git and readable only by the deploying user.
Then make a missing value stop the server before it starts:
const required = ["DATABASE_URL", "SESSION_SECRET"];
const missing = required.filter((name) => !process.env[name]);
if (missing.length > 0) {
console.error(`Missing environment variables: ${missing.join(", ")}`);
process.exit(1);
}Started without the variables, the test container printed Missing environment variables: DATABASE_URL, SESSION_SECRET and exited with 1; started with --env-file, it served requests.
Environment variables undefined after deploy: find your case
| Symptom | Likely cause | Fix |
|---|---|---|
A VITE_ or NEXT_PUBLIC_ value is undefined in the browser | Not set when the build ran | Set it in the build: ARG and --build-arg, or the CI build job's environment |
| You changed a public value on the server and nothing changed | The old value is compiled into the files | Rebuild and redeploy |
Works locally, undefined in CI or Docker builds | The value was only in a gitignored .env | Provide it to the build explicitly |
A non-prefixed variable is undefined in the browser | By design: only prefixed variables reach client code | Keep it server-side, or rename it if it is truly public |
A Next.js server component shows the build's value, or undefined | The route was rendered at build time | Make that part dynamic (connection(), cookies or headers) |
process.env.X is undefined in your API | The container was not given it | env_file, --env-file or -e, plus a startup check |
The app calls localhost in production | A development URL was baked into the build | Set the production URL at build time: see errors after the first deploy |
Public variables are public
Every value in a VITE_ or NEXT_PUBLIC_ variable is readable in the JavaScript your visitors download; Vite's docs say these variables "should not contain sensitive information such as API keys". A Supabase (opens in a new tab) URL and its publishable key belong there; a Supabase secret key, a Stripe secret key or a database password never do. The Supabase anon key article explains which Supabase key is which. If a secret did reach a public variable, treat it as leaked and rotate it, as scanning a repository for secrets describes.
Build arguments are not a place for secrets either. Docker's docs call them "inappropriate for passing secrets", because they can persist in the image's metadata, history and provenance attestations. A secret the build really needs, such as a private registry token, belongs in a secret mount.
Doing this with OpsCrew
OpsCrew sorts the two kinds for you when it builds an image. Variables named with NEXT_PUBLIC_, VITE_, REACT_APP_, PUBLIC_, NUXT_PUBLIC_, EXPO_PUBLIC_ or GATSBY_ go into the build; everything else reaches the app only when it runs on the server. npx opscrew provision stores the values as GitHub Actions (opens in a new tab) secrets; with a Dockerfile, build values not marked secret become build arguments, and those marked secret: true in opscrew/plan.yaml reach the build only as secret mounts. See values the build needs.
Next on the checklist
With the variables in the right place, the production checklist for a vibe-coded app moves on to the errors a first deploy tends to show: 404 on refresh, CORS and 502. If your app came from Lovable (opens in a new tab), deploying a Lovable app to your own server lists its exact Supabase variables.
Key facts
| Fact | Value | Source |
|---|---|---|
| Vite's public prefix | VITE_Only VITE_ variables are exposed to client code, replaced statically at build time. envPrefix changes the prefix. | Env variables and modes (opens in a new tab)Verified on |
| Next.js public prefix | NEXT_PUBLIC_Inlined into the browser bundle during next build. "After being built, your app will no longer respond to changes to these environment variables." | How to use environment variables in Next.js (opens in a new tab)Verified on |
Frequently asked questions
Can I change a VITE_ or NEXT_PUBLIC_ value without rebuilding?
No. The value is part of the built files. Either build once per environment, or have the browser get the value from your server at run time.
How do I pass environment variables to a Docker container?
For values the app reads while it runs, use docker run -e NAME=value, docker run --env-file file, or env_file and environment in Docker Compose. For values the build needs, declare ARG in the Dockerfile and pass --build-arg; they are visible in the image history, so never use them for secrets.
Sources
Related articles
- Deploying
Deploy a Bolt.new app to your own server
How to deploy a Bolt.new app outside Bolt Cloud: get the code, find out which stack Bolt chose, build it in Docker, serve it over HTTPS, and claim the database into Supabase.
- Deploying
Deploy a Lovable app to your own server (Vite or TanStack Start)
How to deploy a Lovable app to a server you control: tell the Vite and TanStack Start stacks apart, sync it to GitHub, build it in Docker, serve it over HTTPS, and keep or move the Supabase backend.
- Deploying
Deploy Next.js to a VPS with Docker and HTTPS
A tested setup for Next.js 16 on your own server: standalone output, a small multi-stage Docker image, PostgreSQL and Caddy with automatic HTTPS, and the build-time traps that break apps after the first deploy.