{"id":11389,"date":"2014-01-24T14:03:11","date_gmt":"2014-01-24T08:33:11","guid":{"rendered":"https:\/\/2thenew.xyz\/blog\/?p=11389"},"modified":"2014-01-24T14:03:11","modified_gmt":"2014-01-24T08:33:11","slug":"sandboxing-in-node-js-using-vm-module","status":"publish","type":"post","link":"https:\/\/2thenew.xyz\/blog\/sandboxing-in-node-js-using-vm-module\/","title":{"rendered":"Sandboxing In Node.JS Using VM Module"},"content":{"rendered":"<p>Two commonly known ways to execute a script in Node.JS are using eval () function or running it using VM module.<\/p>\n<p>Lets see this through an example:<br \/>\n[js]<br \/>\nvar vm = require(&#8216;vm&#8217;);<\/p>\n<p>this.name = &quot;Sakshi&quot;;<\/p>\n<p>var script = &quot;this.name = &#8216;Tyagi&#8217;&quot;, withVM, withEVAL;<\/p>\n<p>withEVAL = eval(script);<\/p>\n<p>console.log(&quot;withEVAL :&quot; + withEVAL + &quot;, &quot; + &quot;local :&quot;+ this.name);<\/p>\n<p>withVM = vm.runInThisContext(script);<\/p>\n<p>console.log(&quot; withVM :&quot; + withVM + &quot;, &quot;+ &quot;local :&quot;+ this.name);<\/p>\n<p>[\/js]<\/p>\n<p>Here, we have executed a piece of JavaScript code in two ways:-<\/p>\n<p>1. <strong>With eval()<\/strong>  = eval()  function passes a string to the JavaScript compiler and execute it. But at the same time, its not safe way to execute a script because it compromises the security of the application. eval() has access to all local as well as global objects.<br \/>\nIn this case output will be:<br \/>\n<code>withEVAL :Tyagi, local :Tyagi<\/code><\/p>\n<p>2. <strong>With vm module<\/strong> = VM provides an access to V8 virtual machine in which the script can be executed. Running script does not have access to local objects. So in this way <code>vm.runInThisContext<\/code> creates its own environment where only global objects can be used, thereby not effecting or accidentally modifying local objects.<br \/>\n In this case output will be:<br \/>\n<code>withVM :Tyagi, local :Sakshi<\/code><\/p>\n<p>So in this way, VM module can be used to sandbox our application having crucial data.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Two commonly known ways to execute a script in Node.JS are using eval () function or running it using VM module. Lets see this through an example: [js] var vm = require(&#8216;vm&#8217;); this.name = &quot;Sakshi&quot;; var script = &quot;this.name = &#8216;Tyagi&#8217;&quot;, withVM, withEVAL; withEVAL = eval(script); console.log(&quot;withEVAL :&quot; + withEVAL + &quot;, &quot; + &quot;local [&hellip;]<\/p>\n","protected":false},"author":65,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4,"footnotes":""},"categories":[1],"tags":[1124],"class_list":["post-11389","post","type-post","status-publish","format-standard","hentry","category-technology","tag-node-js"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Two commonly known ways to execute a script in Node.JS are using eval () function or running it using VM module. Lets see this through an example: [js] var vm = require(&#039;vm&#039;); this.name = &quot;Sakshi&quot;; var script = &quot;this.name = &#039;Tyagi&#039;&quot;, withVM, withEVAL; withEVAL = eval(script); console.log(&quot;withEVAL :&quot; + withEVAL + &quot;, &quot; + &quot;local\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Sakshi Tyagi\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.xyz\/blog\/sandboxing-in-node-js-using-vm-module\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Sandboxing In Node.JS Using VM Module | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"Two commonly known ways to execute a script in Node.JS are using eval () function or running it using VM module. Lets see this through an example: [js] var vm = require(&#039;vm&#039;); this.name = &quot;Sakshi&quot;; var script = &quot;this.name = &#039;Tyagi&#039;&quot;, withVM, withEVAL; withEVAL = eval(script); console.log(&quot;withEVAL :&quot; + withEVAL + &quot;, &quot; + &quot;local\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.xyz\/blog\/sandboxing-in-node-js-using-vm-module\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Sandboxing In Node.JS Using VM Module | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Two commonly known ways to execute a script in Node.JS are using eval () function or running it using VM module. Lets see this through an example: [js] var vm = require(&#039;vm&#039;); this.name = &quot;Sakshi&quot;; var script = &quot;this.name = &#039;Tyagi&#039;&quot;, withVM, withEVAL; withEVAL = eval(script); console.log(&quot;withEVAL :&quot; + withEVAL + &quot;, &quot; + &quot;local\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/sandboxing-in-node-js-using-vm-module\\\/#article\",\"name\":\"Sandboxing In Node.JS Using VM Module | TO THE NEW Blog\",\"headline\":\"Sandboxing In Node.JS Using VM Module\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2014-01-24T14:03:11+05:30\",\"dateModified\":\"2014-01-24T14:03:11+05:30\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/sandboxing-in-node-js-using-vm-module\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/sandboxing-in-node-js-using-vm-module\\\/#webpage\"},\"articleSection\":\"Technology, node.js\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/sandboxing-in-node-js-using-vm-module\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"position\":2,\"name\":\"Technology\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/sandboxing-in-node-js-using-vm-module\\\/#listItem\",\"name\":\"Sandboxing In Node.JS Using VM Module\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/sandboxing-in-node-js-using-vm-module\\\/#listItem\",\"position\":3,\"name\":\"Sandboxing In Node.JS Using VM Module\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/technology\\\/#listItem\",\"name\":\"Technology\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/\",\"name\":\"Sakshi Tyagi\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/sandboxing-in-node-js-using-vm-module\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a42c6deb088e18815dfcfed629c77b032d17bf973881421880b9c5e4ba713739?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Sakshi Tyagi\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/sandboxing-in-node-js-using-vm-module\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/sandboxing-in-node-js-using-vm-module\\\/\",\"name\":\"Sandboxing In Node.JS Using VM Module | TO THE NEW Blog\",\"description\":\"Two commonly known ways to execute a script in Node.JS are using eval () function or running it using VM module. Lets see this through an example: [js] var vm = require('vm'); this.name = \\\"Sakshi\\\"; var script = \\\"this.name = 'Tyagi'\\\", withVM, withEVAL; withEVAL = eval(script); console.log(\\\"withEVAL :\\\" + withEVAL + \\\", \\\" + \\\"local\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/sandboxing-in-node-js-using-vm-module\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/sakshi\\\/#author\"},\"datePublished\":\"2014-01-24T14:03:11+05:30\",\"dateModified\":\"2014-01-24T14:03:11+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Sandboxing In Node.JS Using VM Module | TO THE NEW Blog","description":"Two commonly known ways to execute a script in Node.JS are using eval () function or running it using VM module. Lets see this through an example: [js] var vm = require('vm'); this.name = \"Sakshi\"; var script = \"this.name = 'Tyagi'\", withVM, withEVAL; withEVAL = eval(script); console.log(\"withEVAL :\" + withEVAL + \", \" + \"local","canonical_url":"https:\/\/2thenew.xyz\/blog\/sandboxing-in-node-js-using-vm-module\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.xyz\/blog\/sandboxing-in-node-js-using-vm-module\/#article","name":"Sandboxing In Node.JS Using VM Module | TO THE NEW Blog","headline":"Sandboxing In Node.JS Using VM Module","author":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/sakshi\/#author"},"publisher":{"@id":"https:\/\/2thenew.xyz\/blog\/#organization"},"datePublished":"2014-01-24T14:03:11+05:30","dateModified":"2014-01-24T14:03:11+05:30","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/2thenew.xyz\/blog\/sandboxing-in-node-js-using-vm-module\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.xyz\/blog\/sandboxing-in-node-js-using-vm-module\/#webpage"},"articleSection":"Technology, node.js"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.xyz\/blog\/sandboxing-in-node-js-using-vm-module\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.xyz\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/technology\/#listItem","name":"Technology"}},{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/technology\/#listItem","position":2,"name":"Technology","item":"https:\/\/2thenew.xyz\/blog\/category\/technology\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/sandboxing-in-node-js-using-vm-module\/#listItem","name":"Sandboxing In Node.JS Using VM Module"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/sandboxing-in-node-js-using-vm-module\/#listItem","position":3,"name":"Sandboxing In Node.JS Using VM Module","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/technology\/#listItem","name":"Technology"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.xyz\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.xyz\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.xyz\/blog\/author\/sakshi\/#author","url":"https:\/\/2thenew.xyz\/blog\/author\/sakshi\/","name":"Sakshi Tyagi","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.xyz\/blog\/sandboxing-in-node-js-using-vm-module\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/a42c6deb088e18815dfcfed629c77b032d17bf973881421880b9c5e4ba713739?s=96&d=mm&r=g","width":96,"height":96,"caption":"Sakshi Tyagi"}},{"@type":"WebPage","@id":"https:\/\/2thenew.xyz\/blog\/sandboxing-in-node-js-using-vm-module\/#webpage","url":"https:\/\/2thenew.xyz\/blog\/sandboxing-in-node-js-using-vm-module\/","name":"Sandboxing In Node.JS Using VM Module | TO THE NEW Blog","description":"Two commonly known ways to execute a script in Node.JS are using eval () function or running it using VM module. Lets see this through an example: [js] var vm = require('vm'); this.name = \"Sakshi\"; var script = \"this.name = 'Tyagi'\", withVM, withEVAL; withEVAL = eval(script); console.log(\"withEVAL :\" + withEVAL + \", \" + \"local","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.xyz\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.xyz\/blog\/sandboxing-in-node-js-using-vm-module\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/sakshi\/#author"},"creator":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/sakshi\/#author"},"datePublished":"2014-01-24T14:03:11+05:30","dateModified":"2014-01-24T14:03:11+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.xyz\/blog\/#website","url":"https:\/\/2thenew.xyz\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.xyz\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Sandboxing In Node.JS Using VM Module | TO THE NEW Blog","og:description":"Two commonly known ways to execute a script in Node.JS are using eval () function or running it using VM module. Lets see this through an example: [js] var vm = require('vm'); this.name = &quot;Sakshi&quot;; var script = &quot;this.name = 'Tyagi'&quot;, withVM, withEVAL; withEVAL = eval(script); console.log(&quot;withEVAL :&quot; + withEVAL + &quot;, &quot; + &quot;local","og:url":"https:\/\/2thenew.xyz\/blog\/sandboxing-in-node-js-using-vm-module\/","og:image":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Sandboxing In Node.JS Using VM Module | TO THE NEW Blog","twitter:description":"Two commonly known ways to execute a script in Node.JS are using eval () function or running it using VM module. Lets see this through an example: [js] var vm = require('vm'); this.name = &quot;Sakshi&quot;; var script = &quot;this.name = 'Tyagi'&quot;, withVM, withEVAL; withEVAL = eval(script); console.log(&quot;withEVAL :&quot; + withEVAL + &quot;, &quot; + &quot;local","twitter:image":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"11389","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-30 06:16:40","updated":"2024-02-29 09:52:22","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.xyz\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.xyz\/blog\/category\/technology\/\" title=\"Technology\">Technology<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tSandboxing In Node.JS Using VM Module\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.xyz\/blog"},{"label":"Technology","link":"https:\/\/2thenew.xyz\/blog\/category\/technology\/"},{"label":"Sandboxing In Node.JS Using VM Module","link":"https:\/\/2thenew.xyz\/blog\/sandboxing-in-node-js-using-vm-module\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts\/11389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/comments?post=11389"}],"version-history":[{"count":0,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts\/11389\/revisions"}],"wp:attachment":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/media?parent=11389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/categories?post=11389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/tags?post=11389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}