AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Next.Js Edge Runtime Polyfills Optional Node API Process.GetBuiltinModule With A Throwing Stub, Breaking Import Of Js-Bson

The Edge Runtime process polyfill in Next.js defines process.getBuiltinModule as a throwing stub, violating Node's optional API contract. Packages such as js-bson that use feature detection (e.g., `if (globalThis.process?.getBuiltinModule)`) call the stub and trigger a runtime error, causing 500 errors in edge routes.

highConfidence 92%Next.jsAffected V13.2.5-Canary.7Affected V16.4.0-Canary.15

Origin Analysis

The polyfill in packages/next/src/server/web/sandbox/context.ts includes `getBuiltinModule` in the list of unsupported Node.js APIs that throw an error when accessed. However, Node.js documents `process.getBuiltinModule` as an optional API that may be undefined; code is expected to check for its existence before calling. By providing a throwing getter instead of leaving it undefined, the polyfill breaks valid feature detection and causes crashes.
1. Clone https://github.com/johnmtll/nextjs-edge-import-js-bson-issue-repro 2. Run `npm install` 3. Run `npm run dev` 4. Open http://localhost:3000 5. Observe the error: `A Node.js API is used (process.getBuiltinModule) which is not supported in the Edge Runtime.`

Fixing Code Block

Edge Case Audit

This change may cause packages that previously relied on the thrown error to detect unsupported environments to now silently proceed with an undefined value, potentially leading to different downstream errors if they do not guard correctly. However, this matches the documented Node.js contract for optional APIs. Rollback: revert to the previous throwing stub if unexpected regressions occur in edge runtime packages. Also audit other optional Node.js APIs that may need similar treatment.

Ecosystem Topology