The VM module enables compiling and running code within V8 Virtual Machine contexts.
Example:
Javascript
const util = require( 'util' );
const vm = require( 'vm' );
const contextobj = {
name: 'Nidhi' ,
articles: 60
};
const script = new vm.Script( 'articles *= 10;' );
vm.createContext(contextobj);
script.runInContext(contextobj);
console.log(contextobj);
|
Output:
{ name: 'Nidhi', articles: 600 }
The Complete list of VM are listed below:
Class: vm.Script
VM Methods
VM Methods
|
Description
|
vm.createContext() |
Create a single context that can be utilized to run more than one scripts. |
vm.runInThisContext() |
Runs it inside the context of the current global and then returns the output. |
vm.isContext() |
Check if the stated object is being contextified using vm.createContext() method or not. |
vm.runInContext() |
It runs the code inside the context of the contextifiedObject and then returns the output. |
vm.runInNewContext() |
Compiles the code written and runs |
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Feb, 2023
Like Article
Save Article