Skip to content

Yarn PnP: require.cache and require.extensions are undefined in dependencies (regression in v22.22.3) #64709

Description

@web2ls

Version

v22.22.3 through v22.23.1

Platform

Darwin LMJ6QJLXY 25.5.0 Darwin Kernel Version 25.5.0: Mon Apr 27 20:41:12 PDT 2026; root:xnu-12377.121.6~2/RELEASE_ARM64_T6050 arm64

Subsystem

No response

What steps will reproduce the bug?

#!/usr/bin/env bash
#
# Save this script to a file named `repro.sh`, then run it:
#
#   bash repro.sh
#
# Without an argument, the script creates `node-yarn-pnp-repro` in the
# directory from which the script is run.
#
# Or choose the directory where the test project will be created:
#
#   bash repro.sh ./my-pnp-repro

set -euo pipefail

repro_dir="${1:-node-yarn-pnp-repro}"

if [[ -e "$repro_dir" ]]; then
  echo "Directory already exists: $repro_dir" >&2
  exit 1
fi

mkdir "$repro_dir"
cd "$repro_dir"

echo "Reproduction directory: $PWD"

cat > package.json <<'EOF'
{
  "name": "node-yarn-pnp-repro",
  "private": true,
  "packageManager": "yarn@4.16.0",
  "dependencies": {
    "autoprefixer": "10.4.2",
    "css-loader": "7.1.4",
    "postcss": "8.4.49",
    "postcss-loader": "8.2.1",
    "style-loader": "4.0.0",
    "tailwindcss": "3.4.15",
    "webpack": "5.108.1",
    "webpack-cli": "5.1.4"
  }
}
EOF

cat > .yarnrc.yml <<'EOF'
nodeLinker: pnp
enableGlobalCache: false
EOF

cat > tailwind.config.js <<'EOF'
module.exports = {
  content: ['./src/**/*.html'],
  theme: { extend: {} },
  plugins: [],
};
EOF

cat > postcss.config.js <<'EOF'
module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
};
EOF

mkdir src

cat > src/style.css <<'EOF'
@tailwind base;
@tailwind components;
@tailwind utilities;
EOF

cat > src/index.html <<'EOF'
<div class="text-red-500">hi</div>
EOF

cat > webpack.config.js <<'EOF'
const path = require('path');

module.exports = {
  mode: 'production',
  entry: './src/style.css',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader', 'postcss-loader'],
      },
    ],
  },
};
EOF

corepack yarn install
corepack yarn webpack

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

Activity

  1. dushyant-hada-90 commented on Jul 24, 2026

    @dushyant-hada-90
    Contributor

    I've reproduced this and traced the root cause.

    The regression comes from Yarn PnP's HAS_BROKEN_FSTAT_FOR_ZIP_FDS gate (introduced in 22.22.3) causing its async load hook to return CJS source, which pushes Node into loadCJSModuleWithSpecialRequire. That path builds a synthetic requireFn with only resolve and main no cache or extensions , which is what breaks rechoir and similar packages.

    The fix looks straightforward: attach Module._cache and Module._extensions to the synthetic requireFn in translators.js, mirroring what makeRequireFunction does 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.

  2. MikeMcC399 commented on Jul 24, 2026

    @MikeMcC399
  3. marco-ippolito commented on Jul 24, 2026

    @marco-ippolito
    Member

    require.extensions is deprecated 🫩 do not rely on it

  4. MikeMcC399 commented on Jul 24, 2026

    @MikeMcC399
    Contributor

    Ubuntu 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
  5. MikeMcC399 commented on Jul 24, 2026

    @MikeMcC399
    Contributor

    Upgrading 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-modules in .yarnrc.yml works around the issue.

  6. MikeMcC399 commented on Jul 25, 2026

    @MikeMcC399
    Contributor

    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
  7. ljharb commented on Jul 25, 2026

    @ljharb
    SponsorMember

    If 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.

  8. MikeMcC399 commented on Jul 26, 2026

    @MikeMcC399
    Contributor

    @web2ls

    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 issue

    Yarn 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.

  9. MikeMcC399 commented on Sep 15, 2026

    @MikeMcC399
    Contributor

    A PR yarnpkg/berry#7256 has been proposed to fix yarnpkg/berry#7234 , however it has not received any review.

  10. MikeMcC399 commented on Sep 27, 2026

    @MikeMcC399
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions