Relative `require()` in `commonjs` source not processed by ESM Loader
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.2.0
Platform
Darwin mac.local 23.4.0 Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:49 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6020 arm64
Subsystem
No response
What steps will reproduce the bug?
'use strict';
require('node:module').register(
'data:text/javascript,' + `
import assert from 'node:assert';
export ${encodeURIComponent(
function resolve(specifier, context, nextResolve) {
console.log({ specifier, context });
return nextResolve(specifier, context);
},
)}
export ${encodeURIComponent(
function load(url, context, nextLoad) {
if (url === 'custom:cjs') {
return {
format: 'commonjs',
source: 'console.log(require("./relative"))',
shortCircuit: true,
};
}
return nextLoad(url, context);
}
)}
`,
);
import('custom:cjs').then(console.log, console.error);
How often does it reproduce? Is there a required condition?
Consistently
What is the expected behavior? Why is that the expected behavior?
The load hook doc says:
When a source is provided, all require calls from this module will be processed by the ESM loader with registered resolve and load hooks
What do you see instead?
require() calls with relative path are still processed by the CommonJS loader
Error: Cannot find module './relative'
Require stack:
- custom:cjs
at Module._resolveFilename (node:internal/modules/cjs/loader:1186:15)
at require (node:internal/modules/esm/translators:195:30)
at Object.<anonymous> (custom:cjs:1:13)
at loadCJSModule (node:internal/modules/esm/translators:223:3)
at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:258:7)
at ModuleJob.run (node:internal/modules/esm/module_job:262:25)
at async ModuleLoader.import (node:internal/modules/esm/loader:475:24) {
code: 'MODULE_NOT_FOUND',
requireStack: [ 'custom:cjs' ]
}
Additional information
No response
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- 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 bản tái hiện được cung cấp trên Node.js v22.2.0 và theo dõi các frame ESM loader và CommonJS translator được hiển thị trong stack. Công việc được xem là hoàn tất khi một require tương đối từ mã nguồn CommonJS do hook cung cấp được xử lý thông qua các hook resolve và load đã đăng ký, như được nêu trong tài liệu load-hook.
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, node.js
- Lĩnh vực
- backend
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 42/100