Skip to main content
Script.prototype.runInNewContext - node__vm.d.ts - Node documentation
method Script.prototype.runInNewContext

Usage in Deno

```typescript import { Script } from "node:node__vm.d.ts"; ```
Script.prototype.runInNewContext(
contextObject?: Context,
): any
First contextifies the given `contextObject`, runs the compiled code contained by the `vm.Script` object within the created context, and returns the result. Running code does not have access to local scope. The following example compiles code that sets a global variable, then executes the code multiple times in different contexts. The globals are set on and contained within each individual `context`. ```js import vm from 'node:vm'; const script = new vm.Script('globalVar = "set"'); const contexts = [{}, {}, {}]; contexts.forEach((context) => { script.runInNewContext(context); }); console.log(contexts); // Prints: [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }] ```

Parameters

optional
contextObject: Context
An object that will be `contextified`. If `undefined`, a new object will be created.

Return Type

any
the result of the very last statement executed in the script.