You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot return value from a SourceTextModule to the caller #37453
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.
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.
@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.
Activity
devsnek commentedon Feb 20, 2021
This is working as intended. Can you point me to the location in the docs where it suggests otherwise?
clecap commentedon 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).
devsnek commentedon Feb 20, 2021
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.clecap commentedon Feb 20, 2021
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 :(
devsnek commentedon Feb 20, 2021
@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.doc: document that module.evaluate fulfills as undefined
doc: document that module.evaluate fulfills as undefined
doc: document that module.evaluate fulfills as undefined
doc: document that module.evaluate fulfills as undefined