Node.js | script.runInThisContext() Method Last Updated : 02 Apr, 2020 Comments Improve Suggest changes Like Article Like Report 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 method accepts single parameter options which is optional and returns Object. The options can be displayErrors, timeout, and breakOnSigint. Return Value: It returns the result of the very last statement executed in the script. Below examples illustrate the use of script.runInThisContext() method in Node.js: Example 1: javascript // Node.js program to demonstrate the // script.runInThisContext() method // Including vm module const vm = require('vm'); // Defining code let code = 'console.log("I am an author?");'; // Defining script let script = new vm.Script(code); // Calling runInThisContext method script.runInThisContext(); Output: I am an author? Example 2: javascript // Node.js program to demonstrate the // script.runInThisContext() method // Including vm module const vm = require('vm'); // Defining x and y var x = 40; var y = 17; // Adding x and y const z = x + y; // Dwfining code let code = console.log(z); // Defining script let script = new vm.Script(code); // Calling runInThisContext method script.runInThisContext(); Output: 57 Reference: https://nodejs.org/api/vm.html#vm_script_runinthiscontext_options Comment More infoAdvertise with us Next Article Node.js vm.createContext() Method 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