-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
fix(node): use Module.registerHooks instead of deprecated Module.register for Node v26+ #19907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,8 +14,14 @@ if (!process.versions.bun) { | |
| let localRequire = Module.createRequire(import.meta.url) | ||
|
|
||
| // `Module#register` was added in Node v18.19.0 and v20.6.0 | ||
| // `Module#registerHooks` was added in Node v26.0.0 and is the preferred API | ||
| // | ||
| // Not calling it means that while ESM dependencies don't get reloaded, the | ||
| // actual included files will because they cache bust directly via `?id=…` | ||
| Module.register?.(pathToFileURL(localRequire.resolve('@tailwindcss/node/esm-cache-loader'))) | ||
| let loaderUrl = pathToFileURL(localRequire.resolve('@tailwindcss/node/esm-cache-loader')) | ||
| if (Module.registerHooks) { | ||
| Module.registerHooks({ resolve: loaderUrl }) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is incorrect usage. As per the docs, the exported |
||
| } else { | ||
|
Comment on lines
+21
to
+24
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🌐 Web query:
💡 Result: In Node.js v26, the exact signature of module.registerHooks from the node:module module is: js module.registerHooks(options) Where options is an Object with the following properties: - load {Function|undefined} (optional) The synchronous load hook function. See load hook documentation. Default: undefined. - resolve {Function|undefined} (optional) The synchronous resolve hook function. See resolve hook documentation. Default: undefined. The function returns an object with a deregister method to remove the registered hooks. The resolve option requires a Function type: function resolve(specifier, context, nextResolve) { ... }. It must be synchronous for registerHooks (as opposed to the async version in module.register). It receives a specifier {string}, context {Object}, and nextResolve {Function}, and returns {Object|Promise} (but synchronous in practice for this API). This API registers synchronous, in-thread hooks for customizing module resolution and loading for both ESM and CJS, added in v23.5.0 / v22.15.0, currently at Stability: 1.2 - Release candidate (per v25 docs; stable or higher by v26). Use via --import or --require for early registration, e.g.: js // register-hooks.js import { registerHooks } from 'node:module'; registerHooks({ resolve(specifier, context, nextResolve) { // Custom logic return nextResolve(specifier, context); } }); node --import ./register-hooks.js app.js Official docs: https://nodejs.org/api/module.html#moduleregisterhooksoptions (v25; v26 unchanged per canary/preview). Implementation: nodejs/node@b2e44a8079. Tracking: nodejs/node#56241. Citations:
🏁 Script executed: find . -name "*esm-cache*" -type f | head -20Repository: tailwindlabs/tailwindcss Length of output: 121 🏁 Script executed: cat -n ./packages/@tailwindcss-node/src/esm-cache.loader.mtsRepository: tailwindlabs/tailwindcss Length of output: 784 🏁 Script executed: cat -n ./packages/@tailwindcss-node/src/index.tsRepository: tailwindlabs/tailwindcss Length of output: 1240 🏁 Script executed: # Check Node.js documentation to understand the API design difference
# between registerHooks (sync) and register (async)
cat -n ./packages/@tailwindcss-node/src/index.ts | head -27Repository: tailwindlabs/tailwindcss Length of output: 1240
The 🤖 Prompt for AI Agents |
||
| Module.register?.(loaderUrl) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Source: https://nodejs.org/docs/latest/api/module.html#moduleregisterhooksoptions