Sub topic 21
Building and deploying to production
This chapter assumes you have a working framework mode app with app/routes.ts, loaders, actions, and cookie sessions, and that you know basic npm scripts and environment variables. It covers what react-router build produces, how to run it, how to configure it for your host, and the mistakes that usually appear on the first deploy.
What the build produces
Run the build script from your project root:
npm run buildThis runs react-router build, which uses Vite to compile your app twice and writes two folders:
| Folder | Contents | Who uses it |
|---|---|---|
| `build/client` | Hashed JavaScript, CSS, and static assets from `public/` | The browser, served as static files |
| `build/server` | `index.js`, the compiled server bundle with your loaders, actions, and server-rendering code | A Node.js process |
The exact output depends on react-router.config.ts. With ssr: true, you get both folders. With ssr: false, you get only build/client, because there is no server code to run.
Running the build
The default project template defines these scripts:
{
"scripts": {
"build": "react-router build",
"dev": "react-router dev",
"start": "react-router-serve ./build/server/index.js",
"typecheck": "react-router typegen && react-router typegen && tsc"
}
}react-router-serve comes from the @react-router/serve package. It is a small Express-based server that does three things:
- It serves
build/clientwith long-lived caching for the hashed files under/assets. - It passes every other request to the request handler exported by
build/server/index.js. - It reads the
PORTenvironment variable and defaults to 3000.
Test the production build locally before you deploy:
npm run build
NODE_ENV=production PORT=3000 SESSION_SECRET=local-test-secret npm run startnpm run dev hides several production differences. These include minified output, secure cookies, and server code that is bundled rather than loaded through Vite. Run the built app at least once before every first deploy.
Environment variables
Server code reads process.env at runtime, so your host injects values when the process starts. Do not bake secrets into the build. The session example from the earlier chapter reads process.env.SESSION_SECRET!, so the process fails or signs cookies with undefined if the variable is missing.
Client code works differently. Vite replaces import.meta.env.VITE_* values at build time. Any variable with the VITE_ prefix becomes part of the public JavaScript, so never put a secret there.
- Server-only values such as database URLs and
SESSION_SECRETgo in the host's runtime configuration. - Public values such as an analytics ID can use
VITE_and must be present when you runnpm run build.
Deploying to a Node host
For a container platform, a multi-stage Dockerfile keeps dev dependencies out of the final image:
FROM node:20-alpine AS development-dependencies-env
COPY . /app
WORKDIR /app
RUN npm ci
FROM node:20-alpine AS production-dependencies-env
COPY ./package.json package-lock.json /app/
WORKDIR /app
RUN npm ci --omit=dev
FROM node:20-alpine AS build-env
COPY . /app/
COPY --from=development-dependencies-env /app/node_modules /app/node_modules
WORKDIR /app
RUN npm run build
FROM node:20-alpine
ENV NODE_ENV=production
COPY ./package.json package-lock.json /app/
COPY --from=production-dependencies-env /app/node_modules /app/node_modules
COPY --from=build-env /app/build /app/build
WORKDIR /app
CMD ["npm", "run", "start"]The final image contains build/ and production dependencies only. @react-router/serve must be in dependencies, not devDependencies, or npm ci --omit=dev will remove it. Setting NODE_ENV=production makes the secure flag in the session cookie take effect, so your host must terminate HTTPS in front of the container.
If you need custom middleware, a different runtime, or your own server, skip react-router-serve. Use an adapter such as @react-router/express or @react-router/node and import the build from build/server. The route modules do not change.
Deploying static output
Two configurations need no Node server at runtime:
import type { Config } from "@react-router/dev/config";
export default {
ssr: false,
prerender: ["/", "/about"],
} satisfies Config;- With
ssr: falseand noprerender, the build writes a singleindex.htmlshell inbuild/client(SPA mode). Configure the host to return that file for unknown paths, or deep links such as/contacts/3will return 404 on refresh. - With
prerenderlisted, each path becomes its own HTML file. Pages not in the list still need the SPA fallback.
SPA mode and prerendering run loaders only at build time or not at all. Server loader and action exports are not allowed when ssr: false, except on prerendered routes that only need a loader. Use clientLoader and clientAction for runtime data, and call an API for it.
Common first-deploy errors
- Blank page or 404 on refresh. A static host has no fallback to
index.html. Add a rewrite rule. - Assets return 404. You deployed
build/serveronly, or the host serves from the wrong folder. Browsers needbuild/client. - Cookie never persists.
secure: trueover plain HTTP. Terminate TLS or test withNODE_ENVunset. Cannot find moduleat start. A runtime package was indevDependencies.- Server-only import in client bundle. The build fails if a client-reachable file imports a
.servermodule. Move the import into a loader or action.
Exercise
You set ssr: false and deploy build/client to a static host. Visiting the home page works, but refreshing on /contacts/3 returns 404. What is wrong, and how do you fix it?
Show answer
In SPA mode the build only emits one index.html, and /contacts/3 is not a real file on the host. Configure the host to rewrite unknown paths to /index.html (or prerender the paths you can list), so the client router can match the route.
Exercise
Why should SESSIONSECRET be set on the host instead of being in a VITE variable?
Show answer
Variables prefixed with VITE are inlined into the client JavaScript at build time, so anyone can read them. SESSIONSECRET is read from process.env on the server at runtime and must never reach the browser.