vm.createContext
History
contextObject argument now accepts vm.constants.DONT_CONTEXTIFY.vm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADER.importModuleDynamically option is supported now.microtaskMode option is supported now.codeGeneration option is supported now.vm.createContext(contextObject?, options?): Object
Object | vm.constants.DONT_CONTEXTIFY | undefinedvm.constants.DONT_CONTEXTIFY or an object that will be contextified.
If undefined, an empty contextified object will be created for backwards compatibility.Objectstring'VM Context i', where i is an ascending numerical index of
the created context.stringurl.origin property of a URL object. Most notably, this
string should omit the trailing slash, as that denotes a path.
Default: ''.ObjectstringafterEvaluate, microtasks (tasks
scheduled through Promises and async functions) will be run immediately
after a script has run through script.runInContext().
They are included in the timeout and breakOnSigint scopes in that case.Function | vm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADERimport() is
called in this context without a referrer script or module. This option is
part of the experimental modules API. We do not recommend using it in a
production environment. For detailed information, see
Support of dynamic import() in compilation APIs.ObjectIf the given contextObject is an object, the vm.createContext() method will prepare that
object and return a reference to it so that it can be used in
calls to vm.runInContext() or script.runInContext(). Inside such
scripts, the global object will be wrapped by the contextObject, retaining all of its
existing properties but also having the built-in objects and functions any
standard global object has. Outside of scripts run by the vm module, global
variables will remain unchanged.
import { createContext, runInContext } from 'node:vm'; global.globalVar = 3; const context = { globalVar: 1 }; createContext(context); runInContext('globalVar *= 2;', context); console.log(context); // Prints: { globalVar: 2 } console.log(global.globalVar); // Prints: 3
const { createContext, runInContext } = require('node:vm'); global.globalVar = 3; const context = { globalVar: 1 }; createContext(context); runInContext('globalVar *= 2;', context); console.log(context); // Prints: { globalVar: 2 } console.log(global.globalVar); // Prints: 3
If contextObject is omitted (or passed explicitly as undefined), a new,
empty contextified object will be returned.
When the global object in the newly created context is contextified, it has some quirks
compared to ordinary global objects. For example, it cannot be frozen. To create a context
without the contextifying quirks, pass vm.constants.DONT_CONTEXTIFY as the contextObject
argument. See the documentation of vm.constants.DONT_CONTEXTIFY for details.
The vm.createContext() method is primarily useful for creating a single
context that can be used to run multiple scripts. For instance, if emulating a
web browser, the method can be used to create a single context representing a
window's global object, then run all <script> tags together within that
context.
The provided name and origin of the context are made visible through the
Inspector API.