From 2a2effadf64eea28b701283fbe2c0a2fdd429b3f Mon Sep 17 00:00:00 2001 From: Muhammad Zeeshan <61280174+zeeshan56656@users.noreply.github.com> Date: Tue, 21 Jul 2026 12:33:45 +0500 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> PR-URL: https://github.com/nodejs/node/pull/64112 Reviewed-By: Joyee Cheung --- 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 2cb974eced1..c1caced4b79 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); })(); ```