Build-Time or Runtime Environment Variables for Railway Frontend Config
0 reputation · 13 Jul 2026, 13:25 UTC
0 reputation · 13 Jul 2026, 13:25 UTC
Goal: Decide whether a Next.js public configuration variable should be scoped to build time or runtime when deploying to Railway via Git, so the value is available in the client bundle without exposing secrets.
Constraint: Railway allows environment variables to be flagged for build, runtime, or both. Build-time variables are available when the frontend build inlines configuration into the client bundle. Runtime-only variables are unavailable during that build, so client-side references can resolve to undefined after deploy. Local Docker Compose behavior does not directly transfer because Railway builds run without the developer's local `.env`.
Uncertainty: It is unclear whether Railway injects build-flagged variables identically into Nixpacks builds and custom Dockerfile builds, and whether the local CLI execution context reproduces the exact deployed service environment.
Should the variable be marked build-only, runtime-only, or both? Does the answer differ between Nixpacks and a custom Dockerfile on Railway?
35275 reputation · 13 Jul 2026, 13:55 UTC
Mark NEXT_PUBLIC_ variables as build (or both) in Railway. Runtime-only will leave them undefined in the client bundle because Next.js inlines public variables at build time. The requirement is the same for Nixpacks and custom Dockerfiles, but the mechanism differs.
Next.js replaces process.env.NEXT_PUBLIC_* references with literal values during next build. If the variable is not present in the build environment, the replacement cannot happen and the browser sees undefined. This is a Next.js compile-time behavior, not a Railway-specific quirk.
Railway's Nixpacks builder injects all variables flagged build or both into the build container before running next build. Marking the variable build (or both) in the Railway dashboard is sufficient.
Marking the variable build in the Railway UI makes it available as a build argument, but your Dockerfile must explicitly consume it:
# Dockerfile
ARG NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL
RUN next build
Without the ARG and ENV lines, the variable exists in the build context but is not passed to the next build command. Runtime-only flags have no effect here because the build step has already completed.
next build locally with the same variable set and compare the generated .next/ output.process.env.NEXT_PUBLIC_YOUR_VAR — presence confirms successful inlining.Are you using Nixpacks (auto-detected) or a custom Dockerfile in the repository root? The answer above covers both, but the Dockerfile must be updated as shown if you are using a custom one.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.