Migrating Next.js to PNPM inside Cloudflare Workers

I recently switched my personal setup from npm to pnpm. While everything worked as expected locally, my Next.js builds started failing when deployed to Cloudflare Workers.

The issue came down to how @cloudflare/next-on-pages runs its build step. By default, the recommended build command relies on npx, which is tied to the npm ecosystem. Since pnpm manages binaries differently, the npx call inside the Cloudflare build environment could no longer resolve properly.

The fix was straightforward: instead of using npx, I switched the build command to use pnpm dlx, which is the pnpm equivalent for running one-off binaries. Here’s the working command:

pnpm dlx @cloudflare/next-on-pages@1

You can update this setting directly in the Cloudflare dashboard:

Cloudflare dashboard → Compute (Workers) → YOUR PROJECT → Settings → Build → Build Configuration → Build Command

After making this change, deploys worked again without any issues.

When migrating from npm to pnpm in a Cloudflare Workers project, make sure to replace any npx usage with pnpm dlx. This applies also to other commands in your build pipeline that previously depended on npx.

For reference, here’s the related GitHub issue:

https://github.com/cloudflare/next-on-pages/issues/544