Skip to content

Commit ab9a579

Browse files
committed
fix(require-hook): guard require.extensions accesses for Node 24.15+ Yarn PnP
Node.js v24.15.0 introduced a regression (nodejs/node#61769) where the `require` function exposed to CJS modules loaded via the ESM loader no longer carries the `.extensions` property when the module source comes through a custom loader (e.g. Yarn PnP zip loader). This caused a crash at module evaluation time: TypeError: Cannot read properties of undefined (reading '.js') at require-hook.js:35 The fix guards all `require.extensions` accesses with optional chaining and adds early-return guards in `registerHook`/`deregisterHook` so the build degrades gracefully instead of crashing. No behaviour change on Node versions where `require.extensions` is always defined. Fixes vercel#92935
1 parent 1aeb6b1 commit ab9a579

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

packages/next/src/build/next-config-ts/require-hook.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@ import Module from 'node:module'
33
import { readFileSync } from 'node:fs'
44
import { dirname } from 'node:path'
55

6-
const oldJSHook = require.extensions['.js']
6+
// Node.js v24.15+ with Yarn PnP ESM loader may expose a `require` function
7+
// that does not carry the deprecated `.extensions` property (DEP0007).
8+
// Guard all accesses so next build does not crash in that environment.
9+
const oldJSHook = require.extensions?.['.js']
710
const extensions = ['.ts', '.cts', '.mts', '.cjs', '.mjs']
811

912
export function registerHook(swcOptions: SWCOptions) {
13+
if (!require.extensions) return
14+
1015
// lazy require swc since it loads React before even setting NODE_ENV
1116
// resulting loading Development React on Production
1217
const { transformSync } = require('../swc') as typeof import('../swc')
1318

1419
require.extensions['.js'] = function (mod: any, oldFilename) {
1520
try {
16-
return oldJSHook(mod, oldFilename)
21+
return oldJSHook!(mod, oldFilename)
1722
} catch (error) {
1823
if ((error as NodeJS.ErrnoException).code !== 'ERR_REQUIRE_ESM') {
1924
throw error
@@ -37,13 +42,16 @@ export function registerHook(swcOptions: SWCOptions) {
3742
return _compile.call(this, swc.code, filename)
3843
}
3944

40-
return oldHook(mod, oldFilename)
45+
return oldHook!(mod, oldFilename)
4146
}
4247
}
4348
}
4449

4550
export function deregisterHook() {
46-
require.extensions['.js'] = oldJSHook
51+
if (!require.extensions) return
52+
if (oldJSHook !== undefined) {
53+
require.extensions['.js'] = oldJSHook
54+
}
4755
extensions.forEach((ext) => delete require.extensions[ext])
4856
}
4957

0 commit comments

Comments
 (0)