Skip to content

fix: properly hook builtin modules that require the 'node:' prefix#240

Merged
BridgeAR merged 2 commits intonodejs:mainfrom
trentm:trentm-fix-node-prefixed
Jan 27, 2026
Merged

fix: properly hook builtin modules that require the 'node:' prefix#240
BridgeAR merged 2 commits intonodejs:mainfrom
trentm:trentm-fix-node-prefixed

Conversation

@trentm
Copy link
Contributor

@trentm trentm commented Jan 26, 2026

This fixes an issue with hooking builtin modules that require the node: prefix (https://nodejs.org/api/all.html#all_modules_built-in-modules-with-mandatory-node-prefix), for example node:test.

  • With IITM v2.0.0 and earlier this isn't possible.
  • As of IITM v2.0.1 this is possible, but only accidentally because of the added specifier matching intended for sub-module imports.

Details

Given this can-i-hook-node-test.mjs

import { register } from 'node:module'
import { Hook } from './index.js'

register('./hook.mjs', import.meta.url)
Hook(['node:test'], (mod, name, baseDir) => {
  console.log(`IITM Hook: name=${name}`)
})

import('node:test')

With IITM 2.0.0 or earlier this doesn't work:

% node can-i-hook-node-test.mjs

With IITM 2.0.1 - 2.0.5, this "works", but the name value is wrong. The "working" is accidental: it is passing the moduleName === specifier test.

% node can-i-hook-node-test.mjs
IITM Hook: name=test

After this PR:

% node can-i-hook-node-test.mjs
IITM Hook: name=node:test

@trentm trentm self-assigned this Jan 26, 2026
@trentm trentm requested a review from BridgeAR January 27, 2026 00:26
@BridgeAR BridgeAR merged commit de84589 into nodejs:main Jan 27, 2026
51 checks passed
@trentm trentm deleted the trentm-fix-node-prefixed branch January 27, 2026 21:32
trentm added a commit to trentm/import-in-the-middle that referenced this pull request Jan 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants