nodejs / nodejs/node

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

Đang mở
#64,709 9 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
JavaScript
Star
122k
Fork
37.3k
Merge trung bình
4 ngày 2 giờ
Pull request đã merge (30 ngày)
283

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách chạy repro.sh với Yarn Plug'n'Play trên các phiên bản Node.js bị ảnh hưởng và so sánh với nodeLinker: node-modules cùng các phiên bản được liệt kê không bị ảnh hưởng. Kiểm tra cách hàm require được tạo cho các dependency, với rechoir/index.js là điểm vào được quan sát. Được xem là hoàn tất khi bản build webpack thành công và các hàm require của dependency expose require.cache và require.extensions.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript, nodejs
Lĩnh vực
backend, tooling
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Sôi nổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
52/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.