Part 18 / Topic 5 of 8
Environment variables: missing in production in a way dev usually hides
Starting the exact same standalone server without SESSION_PASSWORD set at all:
$ PORT=3101 node .next/standalone/server.js
▲ Next.js 16.4.6
✓ Ready in 0ms
$ curl -s -o /dev/null -w "home: %{http_code}\n" http://localhost:3101/
home: 500
$ curl -s -o /dev/null -w "quickcart: %{http_code}\n" http://localhost:3101/quickcart
quickcart: 500
⨯ Error: SESSION_PASSWORD is not set — see harness/README's Part 15 setup note.
at module evaluation (.next/server/chunks/[root-of-the-server]__1o-p_i16k4axg._.js:8:88828)
...
at Object.<anonymous> (.next/server/middleware.js:4:3)
/ and /quickcart are not in proxy.ts's own matcher list (section "Common mistakes" below has the full list) — and yet every route returns 500, not just the ones the matcher names. The stack trace's last frame, .next/server/middleware.js, is the tell: Next's production runtime loads the proxy bundle's module graph (which imports session-config.ts, Part 15's own file) regardless of whether the current request's path matches, and session-config.ts's own top-level if (!password) throw (a deliberate "fail loudly, not with a default secret" choice, see Part 15) fires before the matcher check ever gets a chance to decide this request doesn't need the proxy at all. Setting the real secret (section 2's command) fixes every route at once, confirming the cause:
$ SESSION_PASSWORD=<real value> PORT=3101 node .next/standalone/server.js
$ curl -s -o /dev/null -w "home: %{http_code}\n" http://localhost:3101/
home: 200
next dev/next start both load harness/.env.local automatically; node .next/standalone/server.js, run from wherever you copy the standalone folder to, does not — the installed self-hosting.md guide's own "Environment Variables" section says plainly that Next.js supports runtime environment variables specifically so "you can use a singular Docker image that can be promoted through multiple environments with different values," i.e. the expectation is that the deployment platform injects them (a container's env, a platform's secrets manager), not a .env.local file that travels with the code. This harness has no such platform (by this part's own scope), so the honest local equivalent is exporting the variable into the shell before node server.js, exactly as done above.
Done here? The practice set for Part 18 — including the solutions — is on the part page.