Skip to content

doc: fix import.meta example for vm.SourceTextModule#64112

Open
zeeshan56656 wants to merge 1 commit into
nodejs:mainfrom
zeeshan56656:fix/64076-sourcetextmodule-importmeta-example
Open

doc: fix import.meta example for vm.SourceTextModule#64112
zeeshan56656 wants to merge 1 commit into
nodejs:mainfrom
zeeshan56656:fix/64076-sourcetextmodule-importmeta-example

Conversation

@zeeshan56656

Copy link
Copy Markdown
Contributor

The import.meta example for new vm.SourceTextModule() in vm.md does not run as written. It fails in two separate ways:

  • The constructor is missing the context: contextifiedObject option, so the module evaluates in the top context where secret is not defined. Running the snippet throws ReferenceError: 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.
  • The trailing note suggests replacing meta.prop = {} with vm.runInContext('{}', contextifiedObject), but '{}' is parsed as an empty block statement and evaluates to undefined. That makes the following Object.getPrototypeOf(import.meta.prop) throw TypeError: Cannot convert undefined or null to object. 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, so the example and its note both behave as documented.

Fixes: #64076

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>
@nodejs-github-bot nodejs-github-bot added doc Issues and PRs related to the documentations. vm Issues and PRs related to the vm subsystem. labels Jun 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doc Issues and PRs related to the documentations. vm Issues and PRs related to the vm subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SourceTextModule example snippet does not behave as hinted in the docs

2 participants