nextjs - ✅(Solved) Fix experimental.swcEnvOptions is not applied to server-side (Node) SWC compilation [1 pull requests, 1 comments, 2 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#93372Fetched 2026-05-01 05:32:27
View on GitHub
Comments
1
Participants
2
Timeline
4
Reactions
0
Author
Participants
Timeline (top)
commented ×1cross-referenced ×1issue_type_added ×1labeled ×1

Error Message

HTTP/1.1 500 Internal Server Error

Root Cause

Root cause (verified by reading the installed source): In packages/next/src/build/swc/options.ts (webpack path), the isServer branch hardcodes env: { targets: { node: process.versions.node } } and never spreads swcEnvOptions, while the client branch correctly does env: { targets: supportedBrowsers, ...swcEnvOptions }.

Fix Action

Fixed

PR fix notes

PR #93395: fix(swc): apply swcEnvOptions to server-side env

Description (problem / solution / changelog)

What?

Apply experimental.swcEnvOptions to the server-side SWC configuration in getLoaderSWCOptions.

Why?

Currently, swcEnvOptions are only applied to client-side compilation, leading to inconsistent behavior between client and server builds.

This PR ensures that server-side SWC compilation respects the same configuration, while preserving the existing Node target behavior.

How?

  • Extract targets from swcEnvOptions
  • Merge remaining fields into the server env configuration
  • Safely merge targets to preserve the default Node target

Notes

  • Scope is intentionally limited to the webpack SWC loader path
  • Jest and other SWC configuration paths are unchanged
  • No tests added since this is a small, isolated configuration fix and existing SWC behavior is covered indirectly through integration tests

Fixes #93372

Changed files

  • packages/next/src/build/swc/options.ts (modified, +4/-0)

Code Example

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.2.0
Binaries:
  Node: 22.22.2
  npm: 10.9.7
  pnpm: 10.33.2
Relevant Packages:
  next: 16.3.0-canary.5
  react: 19.2.4
  react-dom: 19.2.4
  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/izemo/izemo-next-app

To Reproduce

  1. Clone the reproduction repo and pnpm install.
  2. The repo is configured with:
    • next: 16.3.0-canary.5
    • engines.node: "22.22.2"
    • .browserslistrc: chrome 110
    • next.config.ts: experimental.swcEnvOptions: { mode: "usage", coreJs: "3.49" }
  3. app/server/page.tsx is a Server Component (with export const dynamic = "force-dynamic") that calls RegExp.escape("a.b.c"). RegExp.escape is TC39 Stage 4 (ES2025) and is missing in both Chrome 110 and Node 22.22.2; it landed in Chrome 136 / Node 24.
  4. app/client/page.tsx is the client-side mirror (call inside useEffect).
  5. Run pnpm build --webpack and pnpm start.
  6. curl -i http://localhost:3000/server.

Current vs. Expected behavior

Expected: With experimental.swcEnvOptions: { mode: "usage", coreJs: "3.49" }, SWC's preset-env should auto-inject the core-js polyfill for RegExp.escape into BOTH the client bundle and the Node-side server bundle, since neither browserslist target (chrome 110) nor engines.node (22.22.2) ships it natively.

Actual: The polyfill is injected only into the client bundle; the server bundle emits the call as-is.

Evidence after pnpm build --webpack:

Client bundle (.next/static/chunks/app/client/page-*.js): - escape:function polyfill body → 1 occurrence (core-js inlined) ✅

Server bundle (.next/server/app/server/page.js): - escape:function polyfill body → 0 occurrences ❌ - Verbatim user code in bundle : RegExp.escape("a.b.c")

Hitting the route on Node 22.22.2 fails:

$ curl -i http://localhost:3000/server HTTP/1.1 500 Internal Server Error ... ⨯ TypeError: RegExp.escape is not a function at f (.next/server/app/server/page.js:2:13549) at stringify (<anonymous>) { digest: '891653235' }

Root cause (verified by reading the installed source): In packages/next/src/build/swc/options.ts (webpack path), the isServer branch hardcodes env: { targets: { node: process.versions.node } } and never spreads swcEnvOptions, while the client branch correctly does env: { targets: supportedBrowsers, ...swcEnvOptions }.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.2.0
Binaries:
  Node: 22.22.2
  npm: 10.9.7
  pnpm: 10.33.2
Relevant Packages:
  next: 16.3.0-canary.5
  react: 19.2.4
  react-dom: 19.2.4
  typescript: 5.9.3
Next.js Config:
  output: N/A

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

Turbopack

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

next build (local), next start (local)

Additional context

No response

extent analysis

TL;DR

Update next configuration to properly handle server-side polyfills for RegExp.escape or upgrade to a version of Node that supports it.

Guidance

  • Verify that the issue is indeed caused by the missing polyfill for RegExp.escape in the server bundle by checking the server bundle code.
  • Consider upgrading to a version of Node (e.g., Node 24) that natively supports RegExp.escape to avoid the need for polyfills.
  • If upgrading Node is not feasible, explore modifying the next configuration to correctly inject the polyfill into the server bundle, potentially by adjusting the swcEnvOptions or using a custom getServerSideProps function.
  • Check the next documentation and issues for any existing solutions or workarounds for this specific problem.

Example

No specific code example is provided due to the complexity of the issue and the need for a more in-depth understanding of the next configuration and build process.

Notes

The provided information suggests a bug in the next build process, specifically in how it handles server-side polyfills. The issue might be resolved in future versions of next or by adjusting the project's configuration.

Recommendation

Apply a workaround, such as manually adding the polyfill for RegExp.escape to the server bundle or using a different approach to achieve the desired functionality, until a fixed version of next is available or until Node can be upgraded to a version that supports RegExp.escape.

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 - ✅(Solved) Fix experimental.swcEnvOptions is not applied to server-side (Node) SWC compilation [1 pull requests, 1 comments, 2 participants]