Skip to content

ERR_REQUIRE_ESM on Vercel runtime with "type": "module" monorepo — build succeeds, fails at runtime #91661

@ferc

Description

@ferc

Link to the code that reproduces this issue

https://github.com/ferc/next-16-2-esm-bug

To Reproduce

  1. Clone the repo and deploy to Vercel with root directory set to apps/web and build command cd ../.. && turbo run build --filter={apps/web}... && cd apps/web
  2. Visit the deployed URL (e.g. https://next-16-2-esm-bug.vercel.app/)
  3. The page returns 500 with ERR_REQUIRE_ESM

The repo is a pnpm monorepo with:

  • apps/web: Next.js 16.2.0 app with "type": "module" and turbopack.root set to monorepo root
  • packages/utils: workspace package with "type": "module" that re-exports a file using fs/promises and path
  • packages/storage, packages/ui, packages/ai-service: workspace packages with "type": "module" and dependencies (sentry, drizzle-orm, zod, tanstack, etc.)
  • Two dynamic pages (/ and /login) importing from workspace packages

Current vs. Expected behavior

Current: 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:

  • Works on 16.1.6, fails on 16.2.0
  • Build succeeds on Vercel — only fails at runtime in serverless functions
  • Works locally with next build + next start
  • The issue is intermittent on the test repo but consistent on a larger monorepo with the same setup

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.3.0
Binaries:
  Node: 22.22.1
  npm: 10.9.4
  pnpm: 10.27.0
Relevant Packages:
  next: 16.2.0
  react: 19.2.3
  react-dom: 19.2.3
  typescript: 5.9.3
Next.js Config:
  output: N/A

Which area(s) are affected?

Module Resolution, Turbopack, Output, Runtime

Which stage(s) are affected?

Vercel (Deployed)

Additional context

  • Deployed on Vercel with Node.js 22.x, pnpm workspaces, Turborepo
  • The build compiles successfully — the error only occurs at runtime in Vercel's serverless functions
  • next build locally produces .next/package.json with {"type": "commonjs"} which prevents the ESM error. The runtime error suggests this file is missing or misplaced in the serverless function bundle on Vercel
  • In 16.2.0, packages/next/src/build/adapter/build-complete.ts changed to skip nodeFileTrace for Turbopack builds (if (bundler !== Bundler.Turbopack)), which may affect which files are included in the serverless function bundle

Metadata

Metadata

Assignees

No one assigned

    Labels

    linear: nextConfirmed issue that is tracked by the Next.js team.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions