Skip to content

Cannot return value from a SourceTextModule to the caller #37453

Description

@clecap
  • Version: v15.9.0
  • Platform: macOS Catalina 10.15.6
  • Subsystem: VM

What steps will reproduce the bug?

Make file retu.js.

const vm = require('vm');
const contextifiedSandbox = vm.createContext({ });
(async () => {
  const bar = new vm.SourceTextModule(`19;`, { context: contextifiedSandbox });
  async function linker(specifier, referencingModule) { }
  await bar.link(linker);
  const  result  = await bar.evaluate();
  console.log("Result is: ", result);
})();

Run as node --experimental.vm.modules retu.js

What is the expected behavior?

According to the current state of the documentation I expect an output of Result is: 19

What do you see instead?

Output of: Result is: undefined

Activity

  1. added
    esmIssues and PRs related to the ECMAScript Modules implementation.
    vmIssues and PRs related to the vm subsystem.
    on Feb 20, 2021
  2. devsnek commented on Feb 20, 2021

    @devsnek
    Member

    This is working as intended. Can you point me to the location in the docs where it suggests otherwise?

  3. added
    docIssues and PRs related to Node.js documentation.
    on Feb 20, 2021
  4. clecap commented on Feb 20, 2021

    @clecap
    Author

    It pretty much is possible that I misunderstood the docs. What I found was in https://nodejs.org/dist/latest-v15.x/docs/api/vm.html#vm_module_evaluate_options the information that evaluate should return a Promise (but the docs there do not provide a hint as to how that value is derived and to which value the promise should resolve). My naive expectation is that the value of a module is the value of the last expression in the module, which should be 19. This is the usual convention in several other, similar cases.

    Seems I am not the only one with that problem: A similar question on https://www.titanwolf.org/Network/q/6b88da95-c21a-46dd-bc37-100842de1151/y is still unanswered and the 0.5 line answer in https://stackoverflow.com/questions/60796382/how-to-get-default-export-out-of-vm-sourcetextmodule I admit I do not understand.

    So the issue might as well be an issue with the docs (or with my ability to understand the docs).

  5. devsnek commented on Feb 20, 2021

    @devsnek
    Member

    I think the fix here would be to change the docs to say Returns: <Promise> Fulfills with undefined upon success. Would you like to do so? If not I will get around to it eventually.

  6. clecap commented on Feb 20, 2021

    @clecap
    Author

    What then would be the way to obtain a value from a module and, for example, access a default export?

    Wrt a PR on the doc: I am not so knowledgable on how to do that in github :(

  7. devsnek commented on Feb 20, 2021

    @devsnek
    Member

    @clecap the default export is available via module.namespace.default, though it would be undefined in your example. There is no way to access the "last evaluated expression" as there is with vm.Script.

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

    docIssues and PRs related to Node.js documentation.esmIssues and PRs related to the ECMAScript Modules implementation.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