nextjs - 💡(How to fix) Fix ERR_REQUIRE_ESM on Vercel runtime with "type": "module" monorepo — build succeeds, fails at runtime [5 comments, 5 participants]

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…
GitHub stats
vercel/next.js#91661Fetched 2026-04-08 01:02:40
View on GitHub
Comments
5
Participants
5
Timeline
16
Reactions
7
Author
Timeline (top)
subscribed ×8commented ×5mentioned ×2labeled ×1

Error Message

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.

  • 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

Code Example

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"

---

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
RAW_BUFFERClick to expand / collapse

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

extent analysis

Fix Plan

To resolve the ERR_REQUIRE_ESM issue on Vercel, we need to ensure that the .next/package.json file with {"type": "commonjs"} is properly included in the serverless function bundle.

Here are the steps to fix the issue:

  • Update next.config.js to include the necessary files in the serverless function bundle:
module.exports = {
  //... other configurations ...
  experimental: {
    //... other experimental configurations ...
    externalDir: true, // Add this line
  },
}
  • Verify that the turbo configuration includes the necessary files in the build:
{
  "pipeline": {
    "build": {
      "commands": [
        "cd ../.. && turbo run build --filter={apps/web}... && cd apps/web"
      ],
      "outputs": [
        "apps/web/.next/**",
        "apps/web/node_modules/**"
      ]
    }
  }
}
  • If using a vercel.json configuration file, ensure that the version is set to 2 or higher to support the latest serverless function features:
{
  "version": 2,
  //... other configurations ...
}

Verification

To verify that the fix worked, redeploy the application to Vercel and check that the dynamic pages render correctly without the ERR_REQUIRE_ESM error.

Extra Tips

  • Ensure that the node version used on Vercel is compatible with the next version.
  • If issues persist, try setting the output configuration in next.config.js to standalone or serverless to see if it affects the build.
  • Keep an eye on the Next.js and Vercel documentation for updates on serverless function support and ESM compatibility.

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

nextjs - 💡(How to fix) Fix ERR_REQUIRE_ESM on Vercel runtime with "type": "module" monorepo — build succeeds, fails at runtime [5 comments, 5 participants]