node:module registerHooks resolve context.conditions is Array or Set
Version
v22.17.0
Platform
Microsoft Windows NT 10.0.26100.0 x64
Subsystem
module
What steps will reproduce the bug?
Create these files:
// ModuleA.cjs const os = require('node:os'); module.exports = { hello() { console.log('Hello Folks from', os.platform()); } };
// ModuleB.cjs const ModuleA = require('./ModuleA.cjs'); module.exports = { hello() { ModuleA.hello(); } };
// ModuleC.mjs import { createRequire } from 'node:module'; const require = createRequire(import.meta.url); const ModuleB = require('./ModuleB.cjs'); export default { hello() { ModuleB.hello(); } };
// index.mjs import module from 'node:module'; import { pathToFileURL } from 'node:url'; const basePath = pathToFileURL(process.cwd()).toString(); module.registerHooks({ resolve(specifier, context, nextResolve) { console.log( 'context.conditions is', Object.prototype.toString.call(context.conditions), '-', context.parentURL.replace(basePath, '.'), context.conditions?.has?.('require') && 'required' || context.conditions?.includes?.('import') && 'imported', specifier, ); return nextResolve(specifier, context); }, }); const { default: ModuleC } = await import('./ModuleC.mjs'); ModuleC.hello();
Then do:
Result:
context.conditions is [object Array] - ./index.mjs imported ./ModuleC.mjs
context.conditions is [object Array] - ./ModuleC.mjs imported node:module
context.conditions is [object Set] - ./ModuleC.mjs required ./ModuleB.cjs
context.conditions is [object Set] - ./ModuleB.cjs required ./ModuleA.cjs
context.conditions is [object Set] - ./ModuleA.cjs required node:os
Hello Folks from win32
How often does it reproduce? Is there a required condition?
Consistent every time
What is the expected behavior? Why is that the expected behavior?
context.conditions should be always a Set or an Array
What do you see instead?
context.conditions is a Set if it comes from a require call and it is an Array if it comes from an import statement
Additional information
No response