Node.js script.runInContext() Method
Last Updated :
28 Apr, 2025
Improve
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:
javascript
Output:
javascript
Output:
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:
- 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.
- 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.
- 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.
// 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);
// 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);
{ 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);
// 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);
The output is: { globalVar: 42 }Reference: https://nodejs.org/api/vm.html#vm_script_runincontext_contextifiedobject_options