Repository navigation
Cannot return value from a SourceTextModule to the caller #37453
Description
Activity
- addedesmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.vmIssues and PRs related to the vm subsystem.Issues and PRs related to the vm subsystem.
on Feb 20, 2021 This is working as intended. Can you point me to the location in the docs where it suggests otherwise?
- addeddocIssues and PRs related to Node.js documentation.Issues and PRs related to Node.js documentation.
on Feb 20, 2021 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
evaluateshould 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).
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.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 :(
@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.Reacted by Clemens H. CapReacted by Clemens H. Cap- added a commit that references this issue
on Mar 9, 2021 - added a commit that references this issue
on Mar 16, 2021 - added a commit that references this issue
on May 1, 2021 - added a commit that references this issue
on May 22, 2026
What steps will reproduce the bug?
Make file
retu.js.Run as
node --experimental.vm.modules retu.jsWhat is the expected behavior?
According to the current state of the documentation I expect an output of
Result is: 19What do you see instead?
Output of:
Result is: undefined