Error running npm run preview with OpenNext + Cloudflare (setNextjsServerWorkingDirectory failure)
Hi, I’m trying to run my Next.js app with OpenNext + Cloudflare preview (npm run preview), but I’m hitting a runtime error.
Environment:
Next.js: [^14.0.4]
Node.js: [v22.12.0]
Wrangler: 4.38.0
@opennextjs/cloudflare: ^1.8.3
OS: macOS
Steps I followed from here: https://opennext.js.org/cloudflare/get-started#existing-nextjs-apps
Error shown:
What I checked:
-
.open-next/server-functions/default/ exists and contains handler.mjs, , .next/, etc.
- Running from project root.
Tried cleaning .open-next and rebuilding but got same error.
Question:
- Is this a known issue with OpenNext on Wrangler preview?
- Why would setNextjsServerWorkingDirectory fail even though the folder and files exist?
Any workaround to make the local server run successfully ?
#workers-helpGet Started - OpenNext
Open-source Next.js adapters
5 Replies
I'm having the same issue. I can see that in my .open-next build the
@opennextjs/aws server-adapter.js file is included. This file contains the side-effect of changing the working directory to __dirname which resolves to an empty string in the .wrangler build, and thus crashes.
@satya is your project a Fumadocs project by chance? Mine is.Hey @Wilco , no my project is not a Fumadocs project but instead a nextjs project basically serving static contents (blogs) and some minor api calls.
I somehow resolved this error by doing:
- Deleted the node modules
- Use node version
22.12.0 and then reinstalled the packages .
- Followed the instructions from here: https://developers.cloudflare.com/workers/framework-guides/web-apps/nextjs/#deploy-an-existing-nextjs-project-on-workers
Can you try this ^
Note: Works if your node version is >=22.12.0 , or else it throws some warning related to some deprecated dependencies.Cloudflare Docs
Next.js
Create an Next.js application and deploy it to Cloudflare Workers with Workers Assets.
I think my initial assesment of the AWS adapter's
setNextjsServerWorkingDirectory method being called as a side-effect is correct.
@satya the reason why your fix works is because you probably changed your wrangler.jsonc compatbility date to something earlier than 2025-09-15.
As of that compatibility date (see https://developers.cloudflare.com/workers/configuration/compatibility-flags/#enable-process-v2-implementation ) the enable_nodejs_process_v2 flag is enabled by default which reveals the underlying problem.
For me setting the compatibility date to 2025-09-01 fixed the issueCloudflare Docs
Compatibility flags
Opt into a specific features of the Workers runtime for your Workers project.
I see , thanks wilco
GitHub
[BUG] OpenNext AWS adapter code is included in Cloudflare bundle, b...
Describe the bug The server-adapter.ts file from the OpenNext AWS adapter is included in a Cloudflare OpenNext build. This file contains a side-effect of setting process.chdir(__dirname);. The prob...