Node.js script.runInContext() Method Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like 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: 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. 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: javascript // 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: javascript // 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 Comment More infoAdvertise with us Next Article Node.js script.runInNewContext() Method N nidhi1352singh Follow Improve Article Tags : Web Technologies Node.js Node.js- script-module Similar Reads Node.js Constructor: new vm.Script() Method The Constructor: new vm.Script() method creates a new vm.Script object and compiles the stated code but it does not run the code. Moreover, the compiled vm.Script can run afterwards as many times as required. Here, the code is not connected to any global object, rather it's connected before each run 2 min read 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 ) Parameter 2 min read Node.js script.runInNewContext() Method The script.runInNewContext() method first contextifies the stated contextObject, runs the compiled code inside the vm.Script object within the context created and then returns the output. However, the running code has no access to the local scope. Syntax: script.runInNewContext( contextObject, optio 3 min read Node.js | script.runInThisContext() Method The script.runInThisContext() method runs the compiled code present inside the vm.Script within the context of the current global object. Moreover, running code has no access to local scope, but it has access to the current global object. Syntax: script.runInThisContext( options ) Parameters: This m 2 min read Node.js vm.createContext() Method The vm.createContext() method is used to create a single context that can be utilized to run more than one scripts. Moreover, if the stated contextObject is neglected then a new, empty contextified object is returned. However, if contextObject is stated then, this method will ready that object so th 2 min read Node.js vm.runInThisContext() Method The vm.runInThisContext() method compiles the code, runs it inside the context of the current global and then returns the output. Moreover, the running code has no access to the local scope, but have access to the current global object. Syntax: vm.runInThisContext( code, options ) Parameters: This m 2 min read Node.js vm.isContext() Method The vm.isContext() method is an inbuilt application programming interface of the vm module which is used to check if the stated object is being contextified using vm.createContext() method or not. Syntax: vm.isContext( object ) Parameters: This method accepts single parameter object. Return Value: I 2 min read Node.js vm.runInContext() Method The vm.runInContext() method is used to compile the code. It runs the code inside the context of the contextifiedObject and then returns the output. Moreover, the running code have no access to the local scope and, the contextifiedObject object be contextified formerly using vm.createContext() metho 4 min read Node.js vm.runInNewContext() Method The vm.runInNewContext() method contextifies the stated contextObject, compiles the code written and runs it inside the context created and then after all this returns the output. However, the running code have no access to the local scope. Syntax: vm.runInNewContext( code, contextObject, options ) 5 min read Like