Skip to content

Not fully linked vm.Module throws errors from C++ when instantiated #18571

@TimothyGu

Description

@TimothyGu

Executing

const neverLinked = new vm.Module('import "nonexistent";');
// Note: `await` intentionally omitted as the returned promise will never resolve.
neverLinked.link(() => new Promise(() => {}));

const main = new vm.Module('import "neverLinked";');
await main.link(() => neverLinked);
main.instantiate();

with --experimental-vm-modules throws

internal/vm/Module.js:170
    wrap.instantiate();
         ^
Error: linking error, dependency promises must be resolved on instantiate
    at Module.instantiate (internal/vm/Module.js:170:10)

which does not have the error code that is present on all other errors thrown by instantiate(). In comparison,

new vm.Module('').instantiate();

throws an error with a proper error code:

Error [ERR_VM_MODULE_NOT_LINKED]: Module must be linked before it can be instantiated

The thrown error comes from

wrap.instantiate();

which due to restrictions on how V8's C++ API operates makes it inconvenient to add the error code. The way to fix this would be to wrap a try-catch block around

wrap.instantiate();

to throw a proper errors.Error. The error message can be hard-coded as Linked dependencies must all be linked before instantiation, since this should be the only possible error thrown from this line.


See also #18106.

Metadata

Metadata

Assignees

No one assigned

    Labels

    errorsIssues and PRs related to JavaScript errors originated in Node.js core.vmIssues and PRs related to the vm subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions