doc: fix import.meta example for vm.SourceTextModule#64112
Open
zeeshan56656 wants to merge 1 commit into
Open
Conversation
The import.meta example for new vm.SourceTextModule() in vm.md does not
run as written. It fails in two separate ways.
First, the constructor is missing the context: contextifiedObject
option, so the module evaluates in the top context where secret is not
defined, and the snippet throws ReferenceError: secret is not defined.
Second, the trailing note suggests replacing meta.prop = {} with
vm.runInContext('{}', contextifiedObject), but '{}' is parsed as an
empty block and evaluates to undefined. That makes the following
Object.getPrototypeOf(import.meta.prop) throw TypeError. Wrapping it as
'({})' returns an object, which is what the note intends.
This adds the context option and corrects the suggested replacement to
'({})' in both the mjs and cjs variants.
Fixes: nodejs#64076
Signed-off-by: Muhammad Zeeshan <61280174+zeeshan56656@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
import.metaexample fornew vm.SourceTextModule()invm.mddoes not run as written. It fails in two separate ways:context: contextifiedObjectoption, so the module evaluates in the top context wheresecretis not defined. Running the snippet throwsReferenceError: secret is not defined. The text right above the example already describes the module as belonging to the contextified object, so the option just needs to be present.meta.prop = {}withvm.runInContext('{}', contextifiedObject), but'{}'is parsed as an empty block statement and evaluates toundefined. That makes the followingObject.getPrototypeOf(import.meta.prop)throwTypeError: Cannot convert undefined or null to object. Wrapping it as'({})'returns an object, which is what the note intends.This adds the
contextoption and corrects the suggested replacement to'({})'in both the mjs and cjs variants, so the example and its note both behave as documented.Fixes: #64076