Title: require(esm) always feeds full filepaths to customization hooks but require.resolve doesn't · Issue #57125 · nodejs/node · GitHub
Open Graph Title: require(esm) always feeds full filepaths to customization hooks but require.resolve doesn't · Issue #57125 · nodejs/node
X Title: require(esm) always feeds full filepaths to customization hooks but require.resolve doesn't · Issue #57125 · nodejs/node
Description: Version v23.7.0 Platform Darwin X-MacBook-Pro.local 23.1.0 Darwin Kernel Version 23.1.0: Mon Oct 9 21:27:24 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T6000 arm64 Subsystem module What steps will reproduce the bug? Create a customizat...
Open Graph Description: Version v23.7.0 Platform Darwin X-MacBook-Pro.local 23.1.0 Darwin Kernel Version 23.1.0: Mon Oct 9 21:27:24 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T6000 arm64 Subsystem module What steps wil...
X Description: Version v23.7.0 Platform Darwin X-MacBook-Pro.local 23.1.0 Darwin Kernel Version 23.1.0: Mon Oct 9 21:27:24 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T6000 arm64 Subsystem module What steps wil...
Opengraph URL: https://github.com/nodejs/node/issues/57125
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"require(esm) always feeds full filepaths to customization hooks but require.resolve doesn't","articleBody":"### Version\n\nv23.7.0\n\n### Platform\n\n```text\nDarwin X-MacBook-Pro.local 23.1.0 Darwin Kernel Version 23.1.0: Mon Oct 9 21:27:24 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T6000 arm64\n```\n\n### Subsystem\n\nmodule\n\n### What steps will reproduce the bug?\n\nCreate a customization hook that intercepts `resolve` and log out the arguments while making some CJS with the patched require from being loaded via ESM; notice that randomly even though it is not possible to resolve to `file:` you get a file specifier (added some CJS hooks to show better explanation [note _load called differently in non-requirable ESM CJS]):\n\n```cjs\nconst Module = require('module');\n\n// Save the original loader so we can call it later.\nconst originalLoad = Module._load;\nconst originalResolveFilename = Module._resolveFilename;\n\nModule._resolveFilename = function(request, parent) {\n console.log({_resolveFilename:{request,parent}})\n return 'foo'\n return originalResolveFilename.apply(this, arguments);\n};\nModule._load = function(request, parent, isMain) {\nconsole.log({_load:{request,parent,isMain}})\n const exported = originalLoad.apply(this, arguments);\n return exported;\n};\n\nlet id = 1\nModule.registerHooks({\n resolve(specifier, context, nextResolve) {\n console.dir({resolve:{specifier,context}}, {depth:null})\n if (specifier.startsWith('file:')) {\n debugger;\n }\n return {\n url: `sea:${id++}`,\n shortCircuit: true\n }\n return nextResolve(specifier, context)\n },\n load(url, context, nextLoad) {\n console.dir({load:{url,context}}, {depth:null})\n return {\n source: `\n if (module.id.endsWith(2)) throw Error('bail');\n let dep = \"sea:bare/${String(url).slice(4)}\"\n try {\n require(dep);\n } catch (error) {\n // NOTE: this gives a different path to load!\n console.log('FAILED TO REQUIRE', dep, require.resolve(dep))\n }\n `,\n format: 'commonjs',\n shortCircuit: true\n }\n }\n})\nimport('bare')\n```\n\n\n### How often does it reproduce? Is there a required condition?\n\neverytime\n\n### What is the expected behavior? Why is that the expected behavior?\n\nCJS (either by default or hooked _resolveFilename resolves to a non-file [e.g. `foo`]) and then that value is passed into the loader hooks\n\n### What do you see instead?\n\n1. IDEAL: don't need to hook into _resolveFilename to make resolution work; (removing the above monkey patch to _resolveFilename still causes failure)\n2. [unify logic of](https://github.com/nodejs/node/blame/main/lib/internal/modules/esm/translators.js#L137-L156) so that instrumentation can properly deduce how to respond\n i. due to personal preference I'd suggest if they differ to respect the altered form rather than trying to change it into a file.\n ii. There is some odd logic around WASM and JSON that doesn't match either.\n\n### Additional information\n\n_No response_","author":{"url":"https://github.com/bmeck","@type":"Person","name":"bmeck"},"datePublished":"2025-02-18T16:21:35.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/57125/node/issues/57125"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:1fafdccb-64ea-3da7-58e4-4a47a9815be2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DC94:30F1A4:1947F:21B65:6A4C1056 |
| html-safe-nonce | ff0f7a65d928c57addc82b5a8c01e922b79c8855c48b7d8d18c71258542b7329 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQzk0OjMwRjFBNDoxOTQ3RjoyMUI2NTo2QTRDMTA1NiIsInZpc2l0b3JfaWQiOiIxNTk4NjgwODgwNTQ4NDE3NjIyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 82adcdf7418dd3570f187f0dfeff6699bd6795e46f4ac4fac70b324ecd4963c6 |
| hovercard-subject-tag | issue:2860968001 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/57125/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5b48492c8ea772b1d9ab9cc7e59b1b1e4a2541a90ab2e60758cb9037e9760929/nodejs/node/issues/57125 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5b48492c8ea772b1d9ab9cc7e59b1b1e4a2541a90ab2e60758cb9037e9760929/nodejs/node/issues/57125 |
| og:image:alt | Version v23.7.0 Platform Darwin X-MacBook-Pro.local 23.1.0 Darwin Kernel Version 23.1.0: Mon Oct 9 21:27:24 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T6000 arm64 Subsystem module What steps wil... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | bmeck |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0ccfc9e5281bfe12e38a4d632dc422843e4d5b6757917f7efda2f6567d72fea9 |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 18812876666a23a0912556e224383baa4c84cf8e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width