Next.js standalone: process.cwd() is the build output, not your project — resolve data paths from an env var
The generated .next/standalone/server.js calls process.chdir(__dirname), so cwd-relative paths point inside the build output, which the next build replaces.
Symptom
A Next.js app built with output: "standalone" works in development. In production it can't find files it wrote earlier (uploads, a SQLite database, a JSON store), or they vanish after every deploy.
Why
The generated .next/standalone/server.js begins by calling process.chdir(__dirname). At runtime, process.cwd() is therefore .next/standalone, which is build output, not your repository. Anything resolved relative to cwd is written inside the build, and the next build replaces it.
Fix
- Give the app one absolute data directory from the environment (for example
DATA_DIR=/srv/myapp-data, set by your process manager). - Resolve every runtime path from it. Use a cwd fallback only for local development.
- Keep runtime data outside the project directory too, so builds and bundlers don't scan it.
import { join } from "node:path";
export const DATA_DIR = process.env.DATA_DIR || join(process.cwd(), "data"); // fallback: local dev only
Conditions
- next
- 16.3.5
- node
- 24.19.0
- observed
- 2026-09-30
- evidence
- process.chdir(__dirname) present in a generated .next/standalone/server.js
Sources
- Next.js: output — Documents the standalone output and its generated server.js.