Skip to content

proxy/middleware does not work with ESM only app when deployed to vercel #86434

@mburumaxwell

Description

@mburumaxwell

Link to the code that reproduces this issue

https://github.com/mburumaxwell/dependabot-azure-devops/tree/main/apps/web

To Reproduce

  1. Create next 16 app
  2. Set `"type": "module"
  3. Add proxy.ts with some basic logic
  4. Deploy to vercel
  5. Make a request that

Current vs. Expected behavior

ESM-only should be supported on proxy/middleware too. Instead, I get the error this error from the application logs:

Error [ERR_REQUIRE_ESM]: require() of ES Module /var/task/apps/web/.next/server/middleware.js from /var/task/apps/web/___next_launcher.cjs not supported.
middleware.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename middleware.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /var/task/apps/web/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at /opt/rust/nodejs.js:2:13527
    at Function.Jr (/opt/rust/nodejs.js:2:13905)
    at Ue.e.<computed>.Be._load (/opt/rust/nodejs.js:2:13497)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14) {
  code: 'ERR_REQUIRE_ESM'
}
Node.js process exited with exit status: 1. The logs above can help with debugging the issue.
Error [ERR_REQUIRE_ESM]: require() of ES Module /var/task/apps/web/.next/server/middleware.js from /var/task/apps/web/___next_launcher.cjs not supported.
middleware.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename middleware.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /var/task/apps/web/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at /opt/rust/nodejs.js:2:13527
    at Function.Jr (/opt/rust/nodejs.js:2:13905)
    at Ue.e.<computed>.Be._load (/opt/rust/nodejs.js:2:13497)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14) {
  code: 'ERR_REQUIRE_ESM'
}
Node.js process exited with exit status: 1. The logs above can help with debugging the issue.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.1.0: Mon Oct 20 19:32:41 PDT 2025; root:xnu-12377.41.6~2/RELEASE_ARM64_T6000
  Available memory (MB): 16384
  Available CPU cores: 8
Binaries:
  Node: 24.10.0
  npm: 11.6.0
  Yarn: N/A
  pnpm: 10.13.1
Relevant Packages:
  next: 16.0.3 // Latest available version is detected (16.0.3).
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: 5.9.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Turbopack, Middleware

Which stage(s) are affected? (Select all that apply)

Vercel (Deployed)

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    MiddlewareRelated to Next.js Middleware.TurbopackRelated to Turbopack with Next.js.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions