Open In App

Node.js script.runInContext() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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:




// 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);


Output:

{ name: 'Nidhi', articles: 600 }

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

Example 2:




// 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);


Output:

The output is:  { globalVar: 42 }

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



Last Updated : 11 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads