nextjs - 💡(How to fix) Fix Split build mode (compile → generate) crashes with proxy file convention: unguarded proxy.js→middleware.js rename runs in both passes

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Error Message

Build error occurred Error: ENOENT: no such file or directory, rename '<dist>/.next/server/proxy.js' -> '<dist>/.next/server/middleware.js'

Fix Action

Fix / Workaround

Workaround we ship: between the phases, copy middleware.jsproxy.js and middleware.js.nft.jsonproxy.js.nft.json (guarded, idempotent); the generate pass's rename then succeeds and final artifacts are byte-correct (the NFT content rewrite is a no-op on already-rewritten entries).

Code Example

next build --webpack --experimental-build-mode compile
   next build --webpack --experimental-build-mode generate

---

> Build error occurred
Error: ENOENT: no such file or directory, rename '<dist>/.next/server/proxy.js' -> '<dist>/.next/server/middleware.js'

---

Error: ENOENT: ... rename '.../proxy.js.nft.json' -> '.../middleware.js.nft.json'

---

if (proxyFilePath && bundler !== Bundler.Turbopack) {
    await fs.promises.rename(join(distDir, SERVER_DIRECTORY, 'proxy.js'), join(distDir, SERVER_DIRECTORY, 'middleware.js'));
    await fs.promises.rename(join(distDir, SERVER_DIRECTORY, 'proxy.js.nft.json'), ...);
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

(Internal monorepo — full forensic detail below; happy to build a minimal repro on request, but the mechanism is fully identifiable in source.)

To Reproduce

  1. Next.js 16.1.6, webpack builder, an app using the proxy file convention (src/proxy.ts).
  2. Run the split production build:
    next build --webpack --experimental-build-mode compile
    next build --webpack --experimental-build-mode generate

Current vs. Expected behavior

Current: the generate pass crashes:

> Build error occurred
Error: ENOENT: no such file or directory, rename '<dist>/.next/server/proxy.js' -> '<dist>/.next/server/middleware.js'

and after restoring proxy.js manually, it crashes again on the companion file:

Error: ENOENT: ... rename '.../proxy.js.nft.json' -> '.../middleware.js.nft.json'

Expected: generate completes, as it does in a single-pass next build --webpack.

Mechanism (from next/dist/build/index.js, 16.1.6, ~line 2508)

The proxy→middleware output rename runs at the end of every build pass with no already-renamed guard:

if (proxyFilePath && bundler !== Bundler.Turbopack) {
    await fs.promises.rename(join(distDir, SERVER_DIRECTORY, 'proxy.js'), join(distDir, SERVER_DIRECTORY, 'middleware.js'));
    await fs.promises.rename(join(distDir, SERVER_DIRECTORY, 'proxy.js.nft.json'), ...);

In split mode the compile pass performs the rename (consuming proxy.js/proxy.js.nft.json), then the generate pass unconditionally re-attempts it and ENOENTs.

Workaround we ship: between the phases, copy middleware.jsproxy.js and middleware.js.nft.jsonproxy.js.nft.json (guarded, idempotent); the generate pass's rename then succeeds and final artifacts are byte-correct (the NFT content rewrite is a no-op on already-rewritten entries).

Suggested fix: guard the rename on existsSync(proxy.js) (or skip when the middleware outputs already exist).

Provide environment information

  • Next.js 16.1.6, webpack builder (--webpack), output: 'standalone', pnpm monorepo (app nested at apps/web), Node 22/24 (reproduces on both), Linux + macOS.

Which area(s) are affected?

Build (next build), experimental build mode (compile/generate)

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING