Title: resolve hook is not run for require · Issue #55878 · nodejs/node · GitHub
Open Graph Title: resolve hook is not run for require · Issue #55878 · nodejs/node
X Title: resolve hook is not run for require · Issue #55878 · nodejs/node
Description: Version 23.0.0 Platform Linux hooks 6.8.0-47-generic #47~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Oct 2 16:16:55 UTC 2 x86_64 x86_64 x86_64 GNU/Linux Subsystem customization hooks What steps will reproduce the bug? Set up a module resoluti...
Open Graph Description: Version 23.0.0 Platform Linux hooks 6.8.0-47-generic #47~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Oct 2 16:16:55 UTC 2 x86_64 x86_64 x86_64 GNU/Linux Subsystem customization hooks What steps will rep...
X Description: Version 23.0.0 Platform Linux hooks 6.8.0-47-generic #47~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Oct 2 16:16:55 UTC 2 x86_64 x86_64 x86_64 GNU/Linux Subsystem customization hooks What steps will rep...
Opengraph URL: https://github.com/nodejs/node/issues/55878
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"resolve hook is not run for require","articleBody":"### Version\r\n\r\n23.0.0\r\n\r\n### Platform\r\n\r\n```text\r\nLinux hooks 6.8.0-47-generic #47~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Oct 2 16:16:55 UTC 2 x86_64 x86_64 x86_64 GNU/Linux\r\n```\r\n\r\n\r\n### Subsystem\r\n\r\ncustomization hooks\r\n\r\n### What steps will reproduce the bug?\r\n\r\nSet up a [module resolution hook](https://nodejs.org/docs/latest-v22.x/api/module.html#resolvespecifier-context-nextresolve) that replaces the core `fs` import with `fake.js`. Then try to import and require `fs`, respectively. When importing, `fs` is replaced as expected. When requiring, the hook is never run.\r\n\r\n**For the setup:**\r\n\r\n_register.js:_\r\n```\r\nimport { register } from 'node:module';\r\nregister('./hook.js', import.meta.url);\r\n```\r\n\r\n_hook.js:_\r\n```\r\nimport { fileURLToPath } from 'node:url';\r\nimport { dirname, join } from 'node:path';\r\n\r\nexport async function resolve(specifier, context, nextResolve) {\t\r\n\tconst path = fileURLToPath(import.meta.url);\r\n\tconst dir = dirname(path);\r\n\t\r\n\tif (/fs/.test(specifier)) specifier = join(dir, 'fake.js');\r\n\r\n\treturn nextResolve(specifier, context);\r\n};\r\n```\r\n\r\n_fake.js:_\r\n```\r\nexport function readFileSync() { return 'foo'; }\r\n```\r\n\r\n**Now we're ready for the money part:**\r\n\r\n_index.js_\r\n```\r\nimport {readFileSync} from 'fs';\r\nimport { fileURLToPath } from 'node:url';\r\n\r\nconsole.log(readFileSync(fileURLToPath(import.meta.url), 'utf8')); // 'foo'\r\n```\r\n\r\n_index.cjs:_\r\n```\r\nconst readFileSync = require('fs').readFileSync;\r\n\r\nconsole.log(readFileSync(__filename, 'utf8')); // Prints out the source file\r\n```\r\n\r\nNB: `type` is set to `module` in `package.json`.\r\n\r\n### How often does it reproduce? Is there a required condition?\r\n\r\nCan be reliably reproduced every time.\r\n\r\n### What is the expected behavior? Why is that the expected behavior?\r\n\r\nResolve hook should be run even for `require`, and replace `fs` with `fake.js`. The output should be `foo`. \r\n\r\nI am basing this expectation off of the documentation (emphasis mine):\r\n\r\n[`module#resolve`](https://nodejs.org/docs/latest-v22.x/api/module.html#resolvespecifier-context-nextresolve)\r\n\u003e The resolve hook chain is responsible for telling Node.js where to find and how to cache a given import statement or expression, **or require call**.\r\n\r\n[section \"enabling\"](https://nodejs.org/docs/latest-v22.x/api/module.html#enabling):\r\n\u003e my-app.js can also be CommonJS. Customization hooks will run for any modules that it references via import **(and optionally require)**.\r\n\r\n### What do you see instead?\r\n\r\n`node --import=./register.js index.js` produces `foo`, as expected.\r\n\r\n`node --import=./register.js index.cjs` prints out the source file - bad. Annotating the resolve hook with a `console.log` statement shows it is never run.\r\n\r\n### Additional information\r\n\r\nI have asked about this on [Stack Overflow](https://stackoverflow.com/questions/79122415/resolve-hook-not-run-for-require) but not received any answers.","author":{"url":"https://github.com/BadIdeaException","@type":"Person","name":"BadIdeaException"},"datePublished":"2024-11-16T12:29:35.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":8},"url":"https://github.com/55878/node/issues/55878"}
| 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:a3369f76-deb7-88a7-bf63-32abf0de9d71 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EC1E:381BC9:30BADE:417F45:6A4D8D89 |
| html-safe-nonce | 3182f4bfcb7413668050362c21518aa7c8f451fa538b342fc32c5bca9d489661 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQzFFOjM4MUJDOTozMEJBREU6NDE3RjQ1OjZBNEQ4RDg5IiwidmlzaXRvcl9pZCI6IjgwODk1NzM2NTY4NzM1MDQxMzciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ffd025e734f4ed3dcc4a83e225323c7b6c5fe46c455be878e4506aab17058dab |
| hovercard-subject-tag | issue:2664265010 |
| 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/55878/issue_layout |
| twitter:image | https://opengraph.githubassets.com/eb70680ddc2038b1639e624290b293fc765e928ff1079f331b326484729ea761/nodejs/node/issues/55878 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/eb70680ddc2038b1639e624290b293fc765e928ff1079f331b326484729ea761/nodejs/node/issues/55878 |
| og:image:alt | Version 23.0.0 Platform Linux hooks 6.8.0-47-generic #47~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Oct 2 16:16:55 UTC 2 x86_64 x86_64 x86_64 GNU/Linux Subsystem customization hooks What steps will rep... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | BadIdeaException |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9f8758a3953dfe943439713a6fa4f90d542a3431f10e861ca03dd7f39009f320 |
| 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 | bffd5484f01713a661b03469b77678f72b6574ed |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width