Node.js | script.runInContext() Method

The script.runInContext() method is used to run the compiled code which is present inside the vm.Script object within the stated contextifiedObject and returns the output. However, the running code has no access to the local scope.

Syntax:

script.runInContext( contextifiedObject, options )

Parameters: This method accept two parameters as mentioned above and described below:



  • contextifiedObject: It is a contextified object as returned by the vm.createContext() method.
  • options: It is an optional parameter and returns the object. It holds following parameters:
    1. displayErrors: It holds a Boolean i.e. true if an error is thrown while compiling the code and the line of code because of which an error is thrown is linked to the stack trace. Its by default value is true.
    2. timeout: It holds an integer value that specifies the number of milliseconds taken in order to execute the stated code before ending the execution. However, if an execution is closed then an error will occur. And the value for this must be a positive integer absolutely.
    3. breakOnSigint: It holds a Boolean. If its true, then the execution will be stopped as soon as SIGINT i.e. (Ctrl+C) is provided and if the execution is stopped then an error is thrown. Its by default value is false.

Return Value: It returns the result of the very last statement executed in the script.

Below examples illustrate the use of script.runInContext() method in Node.js:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the     
// script.runInContext() method
  
// Including util and vm module
const util = require('util');
const vm = require('vm');
  
// Constructing context
const contextobj = {
  name: 'Nidhi',
  articles: 60
};
  
// Constructing script
const script = new vm.Script('articles *= 10;');
  
// Contextifying object
vm.createContext(contextobj);
  
// Calling runInContext method
script.runInContext(contextobj);
  
// Displays output
console.log(contextobj);

chevron_right


Output:

{ name: 'Nidhi', articles: 600 }

Here, articles is 600 as (60*10 = 600).

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the     
// script.runInContext() method
  
// Including util and vm module
const util = require('util');
const vm = require('vm');
  
// Constructing context
const contextobj = { globalVar: 6 };
  
// Constructing script
const script = new vm.Script('globalVar += 12;');
  
// Contextifying object
vm.createContext(contextobj);
  
// Calling for loop
for (let i = 1; i < 4; i++) {
  
// Calling runInContext method
script.runInContext(contextobj);
}
  
// Displays output
console.log("The output is: ", contextobj);

chevron_right


Output:

The output is:  { globalVar: 42 }

Reference: https://nodejs.org/api/vm.html#vm_script_runincontext_contextifiedobject_options




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.