function
vm.runInNewContext
This method is a shortcut to (new vm.Script(code, options)).runInContext(vm.createContext(options), options). If options is a string, then it specifies the filename.
It does several things at once:
- Creates a new context.
- If
contextObjectis an object, contextifies it with the new context. IfcontextObjectis undefined, creates a new object and contextifies it. IfcontextObjectisvm.constants.DONT_CONTEXTIFY, don't contextify anything. - Compiles the code as a
vm.Script - Runs the compield code within the created context. The code does not have access to the scope in which this method is called.
- Returns the result.
The following example compiles and executes code that increments a global variable and sets a new one. These globals are contained in the contextObject.
import { runInNewContext, constants } from 'node:vm';
const contextObject = {
animal: 'cat',
count: 2,
};
runInNewContext('count += 1; name = "kitty"', contextObject);
console.log(contextObject);
// Prints: { animal: 'cat', count: 3, name: 'kitty' }
// This would throw if the context is created from a contextified object.
// vm.constants.DONT_CONTEXTIFY allows creating contexts with ordinary global objects that
// can be frozen.
const frozenContext = runInNewContext(
'Object.freeze(globalThis); globalThis;',
constants.DONT_CONTEXTIFY,
);The JavaScript code to compile and run.
Either vm.constants.DONT_CONTEXTIFY or an object that will be contextified. If undefined, an empty contextified object will be created for backwards compatibility.
the result of the very last statement executed in the script.
Referenced types
interface Context
interface RunningCodeInNewContextOptions
- breakOnSigint?: boolean
If
true, the execution will be terminated whenSIGINT(Ctrl+C) is received. Existing handlers for the event that have been attached viaprocess.on('SIGINT')will be disabled during script execution, but will continue to work after that. If execution is terminated, anErrorwill be thrown. - cachedData?: ArrayBufferView<ArrayBufferLike>
Provides an optional data with V8's code cache data for the supplied source.
- columnOffset?: number
Specifies the column number offset that is displayed in stack traces produced by this script.
- contextOrigin?: string
Origin corresponding to the newly created context for display purposes. The origin should be formatted like a URL, but with only the scheme, host, and port (if necessary), like the value of the
url.originproperty of aURLobject. Most notably, this string should omit the trailing slash, as that denotes a path. - displayErrors?: boolean
When
true, if anErroroccurs while compiling thecode, the line of code causing the error is attached to the stack trace. - importModuleDynamically?: number | DynamicModuleLoader<Script>
Used to specify how the modules should be loaded during the evaluation of this script when
import()is called. 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 dynamicimport()in compilation APIs. - lineOffset?: number
Specifies the line number offset that is displayed in stack traces produced by this script.
- microtaskMode?: 'afterEvaluate'
If set to
afterEvaluate, microtasks will be run immediately after the script has run. - timeout?: number
Specifies the number of milliseconds to execute code before terminating execution. If execution is terminated, an
Errorwill be thrown. This value must be a strictly positive integer.