Node.js Constructor: new vm.Script() Method Last Updated : 11 Oct, 2021 Comments Improve Suggest changes Like Article Like Report 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, just for that particular run. Syntax: Constructor: new vm.Script( code, options ) Parameters: This method accept two parameters as mentioned above and described below. code: It is the JavaScript code to compile. options: It is optional parameter and it returns Object or string. If it returns a string, then it defines the filename. Below examples illustrate the use of Constructor: new vm.Script() in Node.js: Example 1: javascript // Node.js program to demonstrate the // Constructor: new vm.Script() method // Including vm and util module const util = require('util'); const vm = require('vm'); // Creating context const context = { number: 2 }; // Calling the constructor const script = new vm.Script('Type = "Int"; number *= 2;'); // Contextifying object vm.createContext(context); // Calling runInContext method script.runInContext(context); // Displays output console.log(context); Output: { number: 4, Type: 'Int' } Example 2: javascript // Node.js program to demonstrate the // Constructor: new vm.Script() method // Including vm and util module const util = require('util'); const vm = require('vm'); // Creating context const context = { value: 1.0 }; // Calling the constructor const script = new vm.Script('Type = "Float"; value += 2*0.1;'); // Contextifying object vm.createContext(context); // Calling runInContext method script.runInContext(context); // Displays output console.log(context); Output: { value: 1.2, Type: 'Float' } Reference: https://nodejs.org/api/vm.html#vm_constructor_new_vm_script_code_options Comment More infoAdvertise with us Next Article Node.js script.runInContext() Method N nidhi1352singh Follow Improve Article Tags : Web Technologies Node.js Node.js-vm-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