Skip to content

schemeless builtins crash CJS loader #58607

@bizob2828

Description

@bizob2828

Version

22.15.0

Platform

Darwin TK46XHNX04 24.5.0 Darwin Kernel Version 24.5.0: Tue Apr 22 19:54:49 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6000 arm64

Subsystem

No response

What steps will reproduce the bug?

const { registerHooks } = require('node:module')

registerHooks({
  resolve: (specifier, context, nextResolve) => {
    console.log(`Resolving specifier: ${specifier}`);
    return nextResolve(specifier, context);
  },
  load: (url, context, nextLoad) => {
    console.log(`Loading URL: ${url}`);
    debugger
    return nextLoad(url, context);
  }
})
node -r ./loader-hook.js -e "require('node:test')"

How often does it reproduce? Is there a required condition?

This happens for all the schemeless builtins

const schemelessBlockList = new SafeSet([
  'sea',
  'sqlite',
  'quic',
  'test',
  'test/reporters',
]);

What is the expected behavior? Why is that the expected behavior?

The resolve/loader hooks do not crash

What do you see instead?

❯ node -r ./loader-hook.js -e "require('node:sea')"
Resolving specifier: module
Loading URL: node:module
Resolving specifier: node:sea
Loading URL: sea
node:internal/modules/customization_hooks:276
    throw new ERR_INVALID_RETURN_PROPERTY_VALUE(
    ^

TypeError [ERR_INVALID_RETURN_PROPERTY_VALUE]: Expected a string, an ArrayBuffer, or a TypedArray to be returned for the "source" from the "load" hook but got null.
    at validateLoad (node:internal/modules/customization_hooks:276:11)
    at nextStep (node:internal/modules/customization_hooks:190:14)
    at load (/Users/revans/loader-hook.js:14:12)
    at nextStep (node:internal/modules/customization_hooks:185:26)
    at loadWithHooks (node:internal/modules/customization_hooks:348:18)
    at loadBuiltinWithHooks (node:internal/modules/cjs/loader:1164:24)
    at Function._load (node:internal/modules/cjs/loader:1216:20)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:235:24)
    at Module.require (node:internal/modules/cjs/loader:1487:12) {
  code: 'ERR_INVALID_RETURN_PROPERTY_VALUE'
}

Node.js v22.15.0

Additional information

No response

Activity

joyeecheung

joyeecheung commented on Jun 7, 2025

@joyeecheung
Member

I won’t have time to get back to the computer until next Monday, but it looks like it’s the URL that gets passed into the hooks that should be fixed - it should’ve been node:sea instead of sea, and the non-prefixed one isn’t really an URL so it should’ve fail the validation. The CJS loader internally uses filename and IDs for performance and compatibility everywhere and only convert them into URLs for the hooks when there are hooks, so it seems somewhere the conversion is missed before it goes into the hooks.

To workaround it before the missed conversion is fixed you can prefix the ones that need the scheme with node: manually in the load hook for now.

himself65

himself65 commented on Jun 7, 2025

@himself65
Member

if (StringPrototypeStartsWith(specifier, 'node:')) {
normalized = BuiltinModule.normalizeRequirableId(specifier);
if (!normalized) {
throw new ERR_UNKNOWN_BUILTIN_MODULE(specifier);
}

Seems like issue from here, normalizeRequirableId will translate like node:sea to sea

himself65

himself65 commented on Jun 7, 2025

@himself65
Member

BuiltinModule.canBeRequiredByUsers(StringPrototypeSlice(id, 5)) is always false for those module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @bizob2828@joyeecheung@himself65

      Issue actions

        schemeless builtins crash CJS loader · Issue #58607 · nodejs/node