✦ Continue with Google
AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Experimental.SwcEnvOptions Not Applied To Server-Side SWC Compilation

When using webpack and experimental.swcEnvOptions with mode: 'usage' and coreJs: '3.49', SWC does not apply the env options to server-side Node bundles. This results in missing polyfills for ES2025 features like RegExp.escape, causing a runtime TypeError in server components on Node versions that do not ship the feature.

highConfidence 95%Next.jsAffected V16.3.0-Canary.5

Origin Analysis

In packages/next/src/build/swc/options.ts (webpack path), the isServer branch hardcodes env: { targets: { node: process.versions.node } } and never spreads the swcEnvOptions object, while the client branch correctly uses env: { targets: supportedBrowsers, ...swcEnvOptions }. This omits mode and coreJs settings for the server compilation.
1. Create a Next.js 16.3.0-canary.5 app with engines.node: '22.22.2' and .browserslistrc 'chrome 110'. 2. Configure next.config.ts with experimental.swcEnvOptions: { mode: 'usage', coreJs: '3.49' }. 3. Add a server component with export const dynamic = 'force-dynamic' calling RegExp.escape('a.b.c'). 4. Run pnpm build --webpack and pnpm start. 5. curl -i http://localhost:3000/server; observe HTTP 500 TypeError: RegExp.escape is not a function. 6. Inspect .next/server/app/server/page.js to see the call emitted without core-js polyfill, while the client bundle contains the polyfill.

Fixing Code Block

Edge Case Audit

Spreading swcEnvOptions after targets could override the default Node target if the object contains a targets property, potentially causing incorrect polyfill behavior. To preserve the Node runtime target, consider merging with { ...swcEnvOptions, targets: { node: process.versions.node } } unless users intentionally need to override targets. Additionally, enabling polyfills on the server may increase bundle size and memory usage, and could introduce core-js import resolution issues if core-js is not listed as a dependency. Rollback: remove the added spread from the server branch or pin to a previous Next.js version.

Ecosystem Topology