Repository navigation
Yarn PnP: require.cache and require.extensions are undefined in dependencies (regression in v22.22.3) #64709
Description
Activity
I've reproduced this and traced the root cause.
The regression comes from Yarn PnP's
HAS_BROKEN_FSTAT_FOR_ZIP_FDSgate (introduced in 22.22.3) causing its asyncloadhook to return CJSsource, which pushes Node intoloadCJSModuleWithSpecialRequire. That path builds a syntheticrequireFnwith onlyresolveandmainnocacheorextensions, which is what breaksrechoirand similar packages.The fix looks straightforward: attach
Module._cacheandModule._extensionsto the syntheticrequireFnintranslators.js, mirroring whatmakeRequireFunctiondoes similar to what #62920 did for the sync-hooks case.Would love to take a shot at this if no one else is already on it. Happy to hear if there's a preferred approach before I open a PR.
require.extensionsis deprecated do not rely on itReacted by Jordan HarbandUbuntu 24.04.4 LTS, Node.js 22.23.1
$ corepack yarn webpack [webpack-cli] TypeError: Cannot convert undefined or null to object at Function.keys (<anonymous>) at exports.prepare (/home/mike/github/TESTING/64709-node/.yarn/cache/rechoir-npm-0.8.0-fb660b3bc8-1a30074124.zip/node_modules/rechoir/index.js:21:14) at loadConfigByPath (/home/mike/github/TESTING/64709-node/.yarn/__virtual__/webpack-cli-virtual-3581b48aa1/0/cache/webpack-cli-npm-5.1.4-7be5b53b38-4266909ae5.zip/node_modules/webpack-cli/lib/webpack-cli.js:1377:29) at WebpackCLI.loadConfig (/home/mike/github/TESTING/64709-node/.yarn/__virtual__/webpack-cli-virtual-3581b48aa1/0/cache/webpack-cli-npm-5.1.4-7be5b53b38-4266909ae5.zip/node_modules/webpack-cli/lib/webpack-cli.js:1515:44) at WebpackCLI.createCompiler (/home/mike/github/TESTING/64709-node/.yarn/__virtual__/webpack-cli-virtual-3581b48aa1/0/cache/webpack-cli-npm-5.1.4-7be5b53b38-4266909ae5.zip/node_modules/webpack-cli/lib/webpack-cli.js:1781:33) at WebpackCLI.runWebpack (/home/mike/github/TESTING/64709-node/.yarn/__virtual__/webpack-cli-virtual-3581b48aa1/0/cache/webpack-cli-npm-5.1.4-7be5b53b38-4266909ae5.zip/node_modules/webpack-cli/lib/webpack-cli.js:1877:31) at Command.<anonymous> (/home/mike/github/TESTING/64709-node/.yarn/__virtual__/webpack-cli-virtual-3581b48aa1/0/cache/webpack-cli-npm-5.1.4-7be5b53b38-4266909ae5.zip/node_modules/webpack-cli/lib/webpack-cli.js:944:32) at Command.listener [as _actionHandler] (/home/mike/github/TESTING/64709-node/.yarn/cache/commander-npm-10.0.1-f17613b72b-53f33d8927.zip/node_modules/commander/lib/command.js:482:17) at /home/mike/github/TESTING/64709-node/.yarn/cache/commander-npm-10.0.1-f17613b72b-53f33d8927.zip/node_modules/commander/lib/command.js:1283:65 at Command._chainOrCall (/home/mike/github/TESTING/64709-node/.yarn/cache/commander-npm-10.0.1-f17613b72b-53f33d8927.zip/node_modules/commander/lib/command.js:1177:12)
webpack-cli@7.2.1$ corepack yarn add webpack-cli@^7 corepack yarn webpack ➤ YN0000: · Yarn 4.16.0 ➤ YN0000: ┌ Resolution step ➤ YN0085: │ + webpack-cli@npm:7.2.1, @discoveryjs/json-ext@npm:1.1.0, commander@npm:14.0.3, webpack-merge@npm:6.0.1 ➤ YN0085: │ - @discoveryjs/json-ext@npm:0.5.7, @webpack-cli/configtest@npm:2.1.1, @webpack-cli/info@npm:2.0.2, and 6 more. ➤ YN0000: └ Completed in 1s 478ms ➤ YN0000: ┌ Fetch step ➤ YN0013: │ 4 packages were added to the project, and 9 were removed (+ 98.53 KiB). ➤ YN0000: └ Completed ➤ YN0000: ┌ Link step ➤ YN0092: │ ESM support for PnP uses the experimental loader API and is therefore experimental ➤ YN0000: └ Completed ➤ YN0000: · Done with warnings in 1s 701ms assets by status 5.75 KiB [cached] 1 asset runtime modules 1.14 KiB 4 modules cacheable modules 8.11 KiB modules by path ./.yarn/__virtual__/style-loader-virtual-037d5012e8/0/cache/style-loader-npm-4.0...(truncated) 5.84 KiB ./.yarn/__virtual__/style-loader-virtual-037d5012e8/0/cache/style-loader-npm-4.0...(truncated) 2.42 KiB [built] [code generated] ./.yarn/__virtual__/style-loader-virtual-037d5012e8/0/cache/style-loader-npm-4.0...(truncated) 1.5 KiB [built] [code generated] ./.yarn/__virtual__/style-loader-virtual-037d5012e8/0/cache/style-loader-npm-4.0...(truncated) 1000 bytes [built] [code generated] ./.yarn/__virtual__/style-loader-virtual-037d5012e8/0/cache/style-loader-npm-4.0...(truncated) 298 bytes [built] [code generated] ./.yarn/__virtual__/style-loader-virtual-037d5012e8/0/cache/style-loader-npm-4.0...(truncated) 285 bytes [built] [code generated] ./.yarn/__virtual__/style-loader-virtual-037d5012e8/0/cache/style-loader-npm-4.0...(truncated) 380 bytes [built] [code generated] modules by path ./src/*.css 2.27 KiB ./src/style.css 2.23 KiB [built] [code generated] ./.yarn/__virtual__/css-loader-virtual-38f3f71e78/0/cache/css-loader-npm-7.1.4-f1fc12a1e0-a3a3a6b564.zip/node_modules/css-loader/dist/cjs.js!./.yarn/__virtual__/postcss-loader-virtual-fbc6330124/0/cache/postcss-loader-npm-8.2.1-c201e127e3-8ef4687f05.zip/node_modules/postcss-loader/dist/cjs.js!./src/style.css 39 bytes [built] [code generated] [1 error] ERROR in ./src/style.css (./.yarn/__virtual__/css-loader-virtual-38f3f71e78/0/cache/css-loader-npm-7.1.4-f1fc12a1e0-a3a3a6b564.zip/node_modules/css-loader/dist/cjs.js!./.yarn/__virtual__/postcss-loader-virtual-fbc6330124/0/cache/postcss-loader-npm-8.2.1-c201e127e3-8ef4687f05.zip/node_modules/postcss-loader/dist/cjs.js!./src/style.css) Module build failed (from ./.yarn/__virtual__/postcss-loader-virtual-fbc6330124/0/cache/postcss-loader-npm-8.2.1-c201e127e3-8ef4687f05.zip/node_modules/postcss-loader/dist/cjs.js): TypeError: Cannot read properties of undefined (reading '/home/mike/github/TESTING/64709-node/postcss.config.js') at module.exports (/home/mike/github/TESTING/64709-node/.yarn/cache/import-fresh-npm-3.3.1-1916794950-bf8cc49487.zip/node_modules/import-fresh/index.js:16:33) at Object.loadJsSync (/home/mike/github/TESTING/64709-node/.yarn/__virtual__/cosmiconfig-virtual-bc55bca9a4/0/cache/cosmiconfig-npm-9.0.2-8b99a842bd-132d32863c.zip/node_modules/cosmiconfig/dist/loaders.js:18:12) at .js (/home/mike/github/TESTING/64709-node/.yarn/__virtual__/postcss-loader-virtual-fbc6330124/0/cache/postcss-loader-npm-8.2.1-c201e127e3-8ef4687f05.zip/node_modules/postcss-loader/dist/utils.js:57:56) at #loadConfiguration (/home/mike/github/TESTING/64709-node/.yarn/__virtual__/cosmiconfig-virtual-bc55bca9a4/0/cache/cosmiconfig-npm-9.0.2-8b99a842bd-132d32863c.zip/node_modules/cosmiconfig/dist/Explorer.js:116:42) at #loadConfigFileWithImports (/home/mike/github/TESTING/64709-node/.yarn/__virtual__/cosmiconfig-virtual-bc55bca9a4/0/cache/cosmiconfig-npm-9.0.2-8b99a842bd-132d32863c.zip/node_modules/cosmiconfig/dist/Explorer.js:87:60) at #readConfiguration (/home/mike/github/TESTING/64709-node/.yarn/__virtual__/cosmiconfig-virtual-bc55bca9a4/0/cache/cosmiconfig-npm-9.0.2-8b99a842bd-132d32863c.zip/node_modules/cosmiconfig/dist/Explorer.js:84:88) at async search (/home/mike/github/TESTING/64709-node/.yarn/__virtual__/cosmiconfig-virtual-bc55bca9a4/0/cache/cosmiconfig-npm-9.0.2-8b99a842bd-132d32863c.zip/node_modules/cosmiconfig/dist/Explorer.js:50:40) at async search (/home/mike/github/TESTING/64709-node/.yarn/__virtual__/cosmiconfig-virtual-bc55bca9a4/0/cache/cosmiconfig-npm-9.0.2-8b99a842bd-132d32863c.zip/node_modules/cosmiconfig/dist/Explorer.js:71:28) at async Explorer.search (/home/mike/github/TESTING/64709-node/.yarn/__virtual__/cosmiconfig-virtual-bc55bca9a4/0/cache/cosmiconfig-npm-9.0.2-8b99a842bd-132d32863c.zip/node_modules/cosmiconfig/dist/Explorer.js:78:20) at async loadConfig (/home/mike/github/TESTING/64709-node/.yarn/__virtual__/postcss-loader-virtual-fbc6330124/0/cache/postcss-loader-npm-8.2.1-c201e127e3-8ef4687f05.zip/node_modules/postcss-loader/dist/utils.js:120:18) @ ./src/style.css 8:6-356 20:17-24 25:22-29 25:33-47 25:50-64 24:0-326 24:0-326 webpack 5.108.1 compiled with 1 error in 1067 ms
Reacted by Alexey SlobodyanskyUpgrading does not however solve the issue, it just changes the error message to:
TypeError: Cannot read properties of undefined (reading '/home/mike/github/TESTING/64709-node/postcss.config.js')
Converting to
nodeLinker: node-modulesin.yarnrc.ymlworks around the issue.I can reproduce a breaking change in Node.js 22.22.3 using just rechoir@0.8.0 on its own with Yarn 4 PnP. rechoir@0.8.0 is a direct dependency of webpack-cli.
git clone https://github.com/gulpjs/rechoir cd rechoir npm install corepack@0.35.0 -g npm uninstall yarn -g corepack install yarn@4.17.1 -g corepack enable yarn git clean -xfd # if repeating corepack yarn install corepack yarn test
Node.js Result 22.22.2 pass 22.22.3 Error: EBADF: bad file descriptor, fstat 22.23.1 Error: EBADF: bad file descriptor, fstat 24.18.0 pass 26.5.0 pass Reacted by Alexey SlobodyanskyIf something breaks using PnP, or any tool with a nonstandard package layout, then it's a bug in that tool, not a bug in node.
Backporting #62835, as proposed in #64722, does not appear to resolve the issue.
You may need to open a separate issue for this in the https://github.com/yarnpkg/berry/issues list so it can be looked at from the perspective of Yarn. Your comment yarnpkg/berry#7106 (comment) does not seem to be getting any feedback from the Yarn maintainers or community so far.
Edit: yarnpkg/berry#7234 noticed as new issueYarn Berry's CI test E2E Webpack has been failing for a couple of weeks ("TypeError: Cannot read properties of undefined"), so that would also benefit from some attention from the Yarn maintainers / community to have it working again.
Edit: separately reported as yarnpkg/berry#7233, and now resolved.A PR yarnpkg/berry#7256 has been proposed to fix yarnpkg/berry#7234 , however it has not received any review.
Closing this issue as it should be addressed by Yarn.
Yarn Berry with pnp uses non-standard methods of interacting with the Node.js loader.
Follow up with yarnpkg/berry#7234.
Version
v22.22.3 through v22.23.1
Platform
Subsystem
No response
What steps will reproduce the bug?
How often does it reproduce? Is there a required condition?
Every time with nodeLinker: pnp.
The same project succeeds with nodeLinker: node-modules on the same affected Node.js version.
What is the expected behavior? Why is that the expected behavior?
The build should succeed, as it does on Node.js v22.22.2 and earlier, and on Node.js v24.16.0 and later.
require.cache is documented as an object containing cached CommonJS modules.
Packages using Yarn PnP should continue to receive a require function with the standard CommonJS properties, including require.cache and require.extensions.
What do you see instead?
The build fails while webpack-cli loads webpack.config.js:
[webpack-cli] TypeError: Cannot convert undefined or null to object at Function.keys (<anonymous>) at exports.prepare (.../rechoir/index.js:21:14) at loadConfigByPath (.../webpack-cli/lib/webpack-cli.js:...) ...rechoir calls Object.keys(require.extensions) at that location. Under Yarn PnP on affected Node.js versions, require.extensions is undefined.
The same underlying issue affects packages that access require.cache; for example, code such as delete require.cache[filePath] fails because require.cache is undefined.
Additional information
No response