Skip to content

Provide SourceTextModule sync versions of link and evaluate methods #59656

Description

@SkReD

What is the problem this feature will solve?

Allow to reimplement require(esm) logic with custom require. Current logic in jest covers only require(cjs) and native modules and to support require(esm) there is need of SourceTextModule sync version with sync link and evaluate methods.

jestjs/jest#15275

What is the feature you are proposing to solve the problem?

linkSync and evaluateSync methods for SourceTextModule or another sync way to programmaticaly create and initialize module.

What alternatives have you considered?

Customization hooks but looks like, according to #56241, there is fundamental problem with implementing evaluation hook for esm modules.

Activity

  1. joyeecheung commented on Aug 28, 2025

    @joyeecheung
    Member

    The synchronous linking method is implemented in #59000

    I am not sure if evalauteSync is entirely necessary - evaluation is already synchronous if the module does not contain a top-level await and the returned promise would be synchronously fulfilled with undefined, as specified in ECMA262 (in this case, the promise is just like a maybe, you can just assume that it's already fulfilled in synchronous code).

  2. linked a pull request that will close this issuevm: sync-ify SourceTextModule linkage #59000on Aug 29, 2025
  3. legendecas commented on Aug 29, 2025

    @legendecas
    Member

    I think to be able to implement userland require(esm) on top of vm.SourceTextModule, there must be a way to detect if the module graph is async (like, if it and its dependencies contain TLA or not), so that a userland framework can do:

    function requireEsm() {
      const mod = new vm.SourceTextModule(...);
      mod.linkRequests(mod.moduleRequests.map(resolve));
      mod.instantiate();
    
      // This is missing
      if (mod.isGraphAsync) {
        throw new Error('Module graph is async');
      }
      mod.evaluate();
      if (mod.status === 'errored') {
        throw mod.error;
      }
      return mod.namespace;
    }
  4. added
    vmIssues and PRs related to the vm subsystem.
    on Aug 29, 2025
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

    feature requestIssues requesting new Node.js features.vmIssues and PRs related to the vm subsystem.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions