The issue involves runtime-specific behavior in a monorepo setup with unclear root causes.
The issue occurs when deploying a Next.js 16.2.0 app with `"type": "module"` in a monorepo to Vercel, where the build succeeds but runtime fails with `ERR_REQUIRE_ESM`. The problem appears to be related to the `.next/package.json` boundary not being respected in Vercel's serverless functions, causing Node to fall back to the app's `"type": "module"`. The issue is intermittent in the test repo but consistent in larger monorepos, making it difficult to isolate and reproduce reliably.
https://github.com/ferc/next-16-2-esm-bug
apps/web and build command cd ../.. && turbo run build --filter={apps/web}... && cd apps/webhttps://next-16-2-esm-bug.vercel.app/)ERR_REQUIRE_ESMThe repo is a pnpm monorepo with:
apps/web: Next.js 16.2.0 app with "type": "module" and turbopack.root set to monorepo rootpackages/utils: workspace package with "type": "module" that re-exports a file using fs/promises and pathpackages/storage, packages/ui, packages/ai-service: workspace packages with "type": "module" and dependencies (sentry, drizzle-orm, zod, tanstack, etc.)/ and /login) importing from workspace packagesCurrent: The build compiles successfully on Vercel, but dynamic pages fail at runtime with:
ERR_REQUIRE_ESM: require() of ES Module .next/server/app/page.js
from ___next_launcher.cjs not supported.
page.js is treated as an ES module file as it is a .js file whose nearest parent
package.json contains "type": "module"
next build generates .next/package.json with {"type": "commonjs"} which should override the parent's "type": "module". The error indicates this file is either missing or not found in the serverless function bundle at runtime.
Locally, next build + next start works fine — the issue only occurs when deployed to Vercel.
Expected: Pages should render correctly on Vercel, as they do locally.
Notes:
next build + next startOperating System:
Platfor
Claim this issue to let others know you're working on it. You'll earn 35 points when you complete it!