From b43a0123b9b8873185ce8fbc5ecbeb2cb5d04d79 Mon Sep 17 00:00:00 2001 From: Muhammad Zeeshan <61280174+zeeshan56656@users.noreply.github.com> Date: Wed, 24 Jun 2026 12:48:48 +0000 Subject: [PATCH] doc: fix import.meta example for vm.SourceTextModule 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: https://github.com/nodejs/node/issues/64076 Signed-off-by: Muhammad Zeeshan <61280174+zeeshan56656@users.noreply.github.com> --- doc/api/vm.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/api/vm.md b/doc/api/vm.md index 9c81f7de71479f..2cc13631dbcc3a 100644 --- a/doc/api/vm.md +++ b/doc/api/vm.md @@ -919,6 +919,7 @@ const contextifiedObject = vm.createContext({ secret: 42 }); const module = new vm.SourceTextModule( 'Object.getPrototypeOf(import.meta.prop).secret = secret;', { + context: contextifiedObject, initializeImportMeta(meta) { // Note: this object is created in the top context. As such, // Object.getPrototypeOf(import.meta.prop) points to the @@ -937,7 +938,7 @@ await module.evaluate(); // To fix this problem, replace // meta.prop = {}; // above with -// meta.prop = vm.runInContext('{}', contextifiedObject); +// meta.prop = vm.runInContext('({})', contextifiedObject); ``` ```cjs @@ -947,6 +948,7 @@ const contextifiedObject = vm.createContext({ secret: 42 }); const module = new vm.SourceTextModule( 'Object.getPrototypeOf(import.meta.prop).secret = secret;', { + context: contextifiedObject, initializeImportMeta(meta) { // Note: this object is created in the top context. As such, // Object.getPrototypeOf(import.meta.prop) points to the @@ -964,7 +966,7 @@ const contextifiedObject = vm.createContext({ secret: 42 }); // To fix this problem, replace // meta.prop = {}; // above with - // meta.prop = vm.runInContext('{}', contextifiedObject); + // meta.prop = vm.runInContext('({})', contextifiedObject); })(); ```