UNPKG

learnyounode

Version:

Learn You The Node.js For Much Win! An intro to Node.js via a set of self-guided workshops.

541 lines (506 loc) 133 kB
<!DOCTYPE html><html lang="en"><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>VM (Executing JavaScript) | Node.js v12.10.0 Documentation</title> <link rel="stylesheet" href="data:text/css;base64,LyogbGF0aW4tZXh0ICovCkBmb250LWZhY2UgewogIGZvbnQtZmFtaWx5OiAnTGF0byc7CiAgZm9udC1zdHlsZTogaXRhbGljOwogIGZvbnQtd2VpZ2h0OiA0MDA7CiAgc3JjOiBsb2NhbCgnTGF0byBJdGFsaWMnKSwgbG9jYWwoJ0xhdG8tSXRhbGljJyksIHVybChodHRwczovL2ZvbnRzLmdzdGF0aWMuY29tL3MvbGF0by92MTYvUzZ1OHc0Qk1VVFBIanhzQVVpLXFOaVhnN2VVMC53b2ZmMikgZm9ybWF0KCd3b2ZmMicpOwogIHVuaWNvZGUtcmFuZ2U6IFUrMDEwMC0wMjRGLCBVKzAyNTksIFUrMUUwMC0xRUZGLCBVKzIwMjAsIFUrMjBBMC0yMEFCLCBVKzIwQUQtMjBDRiwgVSsyMTEzLCBVKzJDNjAtMkM3RiwgVStBNzIwLUE3RkY7Cn0KLyogbGF0aW4gKi8KQGZvbnQtZmFjZSB7CiAgZm9udC1mYW1pbHk6ICdMYXRvJzsKICBmb250LXN0eWxlOiBpdGFsaWM7CiAgZm9udC13ZWlnaHQ6IDQwMDsKICBzcmM6IGxvY2FsKCdMYXRvIEl0YWxpYycpLCBsb2NhbCgnTGF0by1JdGFsaWMnKSwgdXJsKGh0dHBzOi8vZm9udHMuZ3N0YXRpYy5jb20vcy9sYXRvL3YxNi9TNnU4dzRCTVVUUEhqeHNBWEMtcU5pWGc3US53b2ZmMikgZm9ybWF0KCd3b2ZmMicpOwogIHVuaWNvZGUtcmFuZ2U6IFUrMDAwMC0wMEZGLCBVKzAxMzEsIFUrMDE1Mi0wMTUzLCBVKzAyQkItMDJCQywgVSswMkM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IC5zaF9jb21tZW50IHsKICBjb2xvcjogIzY2NjsKICBmb250LXdlaWdodDogbGlnaHRlcjsKfQo="> <link rel="canonical" href="https://nodejs.org/api/vm.html"> </head> <body class="alt apidoc" id="api-section-vm"> <div id="content" class="clearfix"> <div id="column2" class="interior"> <div id="intro" class="interior"> <a href="https://nodejs.org/" title="Go back to the home page"> Node.js </a> </div> <ul> <li><a href="https://nodejs.org/api/documentation.html" class="nav-documentation">About these Docs</a></li> <li><a href="https://nodejs.org/api/synopsis.html" class="nav-synopsis">Usage &amp; Example</a></li> </ul> <div class="line"></div> <ul> <li><a href="https://nodejs.org/api/assert.html" class="nav-assert">Assertion Testing</a></li> <li><a href="https://nodejs.org/api/async_hooks.html" class="nav-async_hooks">Async Hooks</a></li> <li><a href="https://nodejs.org/api/buffer.html" class="nav-buffer">Buffer</a></li> <li><a href="https://nodejs.org/api/addons.html" class="nav-addons">C++ Addons</a></li> <li><a href="https://nodejs.org/api/n-api.html" class="nav-n-api">C/C++ Addons - N-API</a></li> <li><a href="https://nodejs.org/api/child_process.html" class="nav-child_process">Child Processes</a></li> <li><a href="https://nodejs.org/api/cluster.html" class="nav-cluster">Cluster</a></li> <li><a href="https://nodejs.org/api/cli.html" class="nav-cli">Command Line Options</a></li> <li><a href="https://nodejs.org/api/console.html" class="nav-console">Console</a></li> <li><a href="https://nodejs.org/api/crypto.html" class="nav-crypto">Crypto</a></li> <li><a href="https://nodejs.org/api/debugger.html" class="nav-debugger">Debugger</a></li> <li><a href="https://nodejs.org/api/deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> <li><a href="https://nodejs.org/api/dns.html" class="nav-dns">DNS</a></li> <li><a href="https://nodejs.org/api/domain.html" class="nav-domain">Domain</a></li> <li><a href="https://nodejs.org/api/esm.html" class="nav-esm">ECMAScript Modules</a></li> <li><a href="https://nodejs.org/api/errors.html" class="nav-errors">Errors</a></li> <li><a href="https://nodejs.org/api/events.html" class="nav-events">Events</a></li> <li><a href="https://nodejs.org/api/fs.html" class="nav-fs">File System</a></li> <li><a href="https://nodejs.org/api/globals.html" class="nav-globals">Globals</a></li> <li><a href="https://nodejs.org/api/http.html" class="nav-http">HTTP</a></li> <li><a href="https://nodejs.org/api/http2.html" class="nav-http2">HTTP/2</a></li> <li><a href="https://nodejs.org/api/https.html" class="nav-https">HTTPS</a></li> <li><a href="https://nodejs.org/api/inspector.html" class="nav-inspector">Inspector</a></li> <li><a href="https://nodejs.org/api/intl.html" class="nav-intl">Internationalization</a></li> <li><a href="https://nodejs.org/api/modules.html" class="nav-modules">Modules</a></li> <li><a href="https://nodejs.org/api/net.html" class="nav-net">Net</a></li> <li><a href="https://nodejs.org/api/os.html" class="nav-os">OS</a></li> <li><a href="https://nodejs.org/api/path.html" class="nav-path">Path</a></li> <li><a href="https://nodejs.org/api/perf_hooks.html" class="nav-perf_hooks">Performance Hooks</a></li> <li><a href="https://nodejs.org/api/policy.html" class="nav-policy">Policies</a></li> <li><a href="https://nodejs.org/api/process.html" class="nav-process">Process</a></li> <li><a href="https://nodejs.org/api/punycode.html" class="nav-punycode">Punycode</a></li> <li><a href="https://nodejs.org/api/querystring.html" class="nav-querystring">Query Strings</a></li> <li><a href="https://nodejs.org/api/readline.html" class="nav-readline">Readline</a></li> <li><a href="https://nodejs.org/api/repl.html" class="nav-repl">REPL</a></li> <li><a href="https://nodejs.org/api/report.html" class="nav-report">Report</a></li> <li><a href="https://nodejs.org/api/stream.html" class="nav-stream">Stream</a></li> <li><a href="https://nodejs.org/api/string_decoder.html" class="nav-string_decoder">String Decoder</a></li> <li><a href="https://nodejs.org/api/timers.html" class="nav-timers">Timers</a></li> <li><a href="https://nodejs.org/api/tls.html" class="nav-tls">TLS/SSL</a></li> <li><a href="https://nodejs.org/api/tracing.html" class="nav-tracing">Trace Events</a></li> <li><a href="https://nodejs.org/api/tty.html" class="nav-tty">TTY</a></li> <li><a href="https://nodejs.org/api/dgram.html" class="nav-dgram">UDP/Datagram</a></li> <li><a href="https://nodejs.org/api/url.html" class="nav-url">URL</a></li> <li><a href="https://nodejs.org/api/util.html" class="nav-util">Utilities</a></li> <li><a href="https://nodejs.org/api/v8.html" class="nav-v8">V8</a></li> <li><a href="https://nodejs.org/api/vm.html" class="nav-vm active">VM</a></li> <li><a href="https://nodejs.org/api/worker_threads.html" class="nav-worker_threads">Worker Threads</a></li> <li><a href="https://nodejs.org/api/zlib.html" class="nav-zlib">Zlib</a></li> </ul> <div class="line"></div> <ul> <li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">GitHub Repo &amp; Issue Tracker</a></li> </ul> </div> <div id="column1" data-id="vm" class="interior"> <header> <h1>Node.js v12.10.0 Documentation</h1> <div id="gtoc"> <ul> <li> <a href="https://nodejs.org/api/index.html" name="toc">Index</a> </li> <li> <a href="https://nodejs.org/api/all.html">View on single page</a> </li> <li> <a href="https://nodejs.org/api/vm.json">View as JSON</a> </li> <li class="version-picker"> <a href="#">View another version <span>▼</span></a> <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v12.x/api/vm.html">12.x</a></li> <li><a href="https://nodejs.org/docs/latest-v11.x/api/vm.html">11.x</a></li> <li><a href="https://nodejs.org/docs/latest-v10.x/api/vm.html">10.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v9.x/api/vm.html">9.x</a></li> <li><a href="https://nodejs.org/docs/latest-v8.x/api/vm.html">8.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v7.x/api/vm.html">7.x</a></li> <li><a href="https://nodejs.org/docs/latest-v6.x/api/vm.html">6.x</a></li> <li><a href="https://nodejs.org/docs/latest-v5.x/api/vm.html">5.x</a></li> <li><a href="https://nodejs.org/docs/latest-v4.x/api/vm.html">4.x</a></li> <li><a href="https://nodejs.org/docs/latest-v0.12.x/api/vm.html">0.12.x</a></li> <li><a href="https://nodejs.org/docs/latest-v0.10.x/api/vm.html">0.10.x</a></li></ol> </li> <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/vm.md"><span class="github_icon"><svg height="16" width="16" viewBox="0 0 16.1 16.1" fill="currentColor"><path d="M8 0a8 8 0 0 0-2.5 15.6c.4 0 .5-.2.5-.4v-1.5c-2 .4-2.5-.5-2.7-1 0-.1-.5-.9-.8-1-.3-.2-.7-.6 0-.6.6 0 1 .6 1.2.8.7 1.2 1.9 1 2.4.7 0-.5.2-.9.5-1-1.8-.3-3.7-1-3.7-4 0-.9.3-1.6.8-2.2 0-.2-.3-1 .1-2 0 0 .7-.3 2.2.7a7.4 7.4 0 0 1 4 0c1.5-1 2.2-.8 2.2-.8.5 1.1.2 2 .1 2.1.5.6.8 1.3.8 2.2 0 3-1.9 3.7-3.6 4 .3.2.5.7.5 1.4v2.2c0 .2.1.5.5.4A8 8 0 0 0 16 8a8 8 0 0 0-8-8z"></path></svg></span>Edit on GitHub</a></li> </ul> </div> <hr> </header> <div id="toc"> <h2>Table of Contents</h2> <ul> <li> <p><span class="stability_2"><a href="#vm_vm_executing_javascript">VM (Executing JavaScript)</a></span></p> <ul> <li> <p><a href="#vm_class_vm_script">Class: vm.Script</a></p> <ul> <li><a href="#vm_constructor_new_vm_script_code_options">Constructor: new vm.Script(code[, options])</a></li> <li><a href="#vm_script_createcacheddata">script.createCachedData()</a></li> <li><a href="#vm_script_runincontext_contextifiedsandbox_options">script.runInContext(contextifiedSandbox[, options])</a></li> <li><a href="#vm_script_runinnewcontext_sandbox_options">script.runInNewContext([sandbox[, options]])</a></li> <li><a href="#vm_script_runinthiscontext_options">script.runInThisContext([options])</a></li> </ul> </li> <li> <p><span class="stability_1"><a href="#vm_class_vm_sourcetextmodule">Class: vm.SourceTextModule</a></span></p> <ul> <li><a href="#vm_constructor_new_vm_sourcetextmodule_code_options">Constructor: new vm.SourceTextModule(code[, options])</a></li> <li><a href="#vm_module_dependencyspecifiers">module.dependencySpecifiers</a></li> <li><a href="#vm_module_error">module.error</a></li> <li><a href="#vm_module_evaluate_options">module.evaluate([options])</a></li> <li><a href="#vm_module_instantiate">module.instantiate()</a></li> <li><a href="#vm_module_link_linker">module.link(linker)</a></li> <li><a href="#vm_module_linkingstatus">module.linkingStatus</a></li> <li><a href="#vm_module_namespace">module.namespace</a></li> <li><a href="#vm_module_status">module.status</a></li> <li><a href="#vm_module_url">module.url</a></li> </ul> </li> <li><a href="#vm_vm_compilefunction_code_params_options">vm.compileFunction(code[, params[, options]])</a></li> <li><a href="#vm_vm_createcontext_sandbox_options">vm.createContext([sandbox[, options]])</a></li> <li><a href="#vm_vm_iscontext_sandbox">vm.isContext(sandbox)</a></li> <li><a href="#vm_vm_runincontext_code_contextifiedsandbox_options">vm.runInContext(code, contextifiedSandbox[, options])</a></li> <li><a href="#vm_vm_runinnewcontext_code_sandbox_options">vm.runInNewContext(code[, sandbox[, options]])</a></li> <li><a href="#vm_vm_runinthiscontext_code_options">vm.runInThisContext(code[, options])</a></li> <li><a href="#vm_example_running_an_http_server_within_a_vm">Example: Running an HTTP Server within a VM</a></li> <li><a href="#vm_what_does_it_mean_to_contextify_an_object">What does it mean to "contextify" an object?</a></li> <li><a href="#vm_timeout_limitations_when_using_process_nexttick_promises_and_queuemicrotask">Timeout limitations when using process.nextTick(), Promises, and queueMicrotask()</a></li> </ul> </li> </ul> </div> <div id="apicontent"> <h1>VM (Executing JavaScript)<span><a class="mark" href="#vm_vm_executing_javascript" id="vm_vm_executing_javascript">#</a></span></h1> <p></p><div class="api_stability api_stability_2"><a href="https://nodejs.org/api/documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p> <p>The <code>vm</code> module provides APIs for compiling and running code within V8 Virtual Machine contexts. <strong>The <code>vm</code> module is not a security mechanism. Do not use it to run untrusted code</strong>. The term "sandbox" is used throughout these docs simply to refer to a separate context, and does not confer any security guarantees.</p> <p>JavaScript code can be compiled and run immediately or compiled, saved, and run later.</p> <p>A common use case is to run the code in a sandboxed environment. The sandboxed code uses a different V8 Context, meaning that it has a different global object than the rest of the code.</p> <p>One can provide the context by <a href="#vm_what_does_it_mean_to_contextify_an_object">"contextifying"</a> a sandbox object. The sandboxed code treats any property in the sandbox like a global variable. Any changes to global variables caused by the sandboxed code are reflected in the sandbox object.</p> <pre><code class="language-js">const vm = require('vm'); const x = 1; const sandbox = { x: 2 }; vm.createContext(sandbox); // Contextify the sandbox. const code = 'x += 40; var y = 17;'; // `x` and `y` are global variables in the sandboxed environment. // Initially, x has the value 2 because that is the value of sandbox.x. vm.runInContext(code, sandbox); console.log(sandbox.x); // 42 console.log(sandbox.y); // 17 console.log(x); // 1; y is not defined. </code></pre> <h2>Class: vm.Script<span><a class="mark" href="#vm_class_vm_script" id="vm_class_vm_script">#</a></span></h2> <div class="api_metadata"> <span>Added in: v0.3.1</span> </div> <p>Instances of the <code>vm.Script</code> class contain precompiled scripts that can be executed in specific sandboxes (or "contexts").</p> <h3>Constructor: new vm.Script(code[, options])<span><a class="mark" href="#vm_constructor_new_vm_script_code_options" id="vm_constructor_new_vm_script_code_options">#</a></span></h3> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.6.0</td> <td><p>The <code>produceCachedData</code> is deprecated in favour of <code>script.createCachedData()</code></p></td></tr> <tr><td>v5.7.0</td> <td><p>The <code>cachedData</code> and <code>produceCachedData</code> options are supported now.</p></td></tr> <tr><td>v0.3.1</td> <td><p><span>Added in: v0.3.1</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> The JavaScript code to compile.</li> <li> <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></p> <ul> <li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> Specifies the filename used in stack traces produced by this script. <strong>Default:</strong> <code>'evalmachine.&lt;anonymous&gt;'</code>.</li> <li><code>lineOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> Specifies the line number offset that is displayed in stack traces produced by this script. <strong>Default:</strong> <code>0</code>.</li> <li><code>columnOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> Specifies the column number offset that is displayed in stack traces produced by this script. <strong>Default:</strong> <code>0</code>.</li> <li><code>cachedData</code> <a href="https://nodejs.org/api/buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&lt;TypedArray&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&lt;DataView&gt;</a> Provides an optional <code>Buffer</code> or <code>TypedArray</code>, or <code>DataView</code> with V8's code cache data for the supplied source. When supplied, the <code>cachedDataRejected</code> value will be set to either <code>true</code> or <code>false</code> depending on acceptance of the data by V8.</li> <li><code>produceCachedData</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a> When <code>true</code> and no <code>cachedData</code> is present, V8 will attempt to produce code cache data for <code>code</code>. Upon success, a <code>Buffer</code> with V8's code cache data will be produced and stored in the <code>cachedData</code> property of the returned <code>vm.Script</code> instance. The <code>cachedDataProduced</code> value will be set to either <code>true</code> or <code>false</code> depending on whether code cache data is produced successfully. This option is <strong>deprecated</strong> in favor of <code>script.createCachedData()</code>. <strong>Default:</strong> <code>false</code>.</li> <li> <p><code>importModuleDynamically</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> Called during evaluation of this module when <code>import()</code> is called. If this option is not specified, calls to <code>import()</code> will reject with <a href="https://nodejs.org/api/errors.html#ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING"><code>ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING</code></a>. This option is part of the experimental API for the <code>--experimental-modules</code> flag, and should not be considered stable.</p> <ul> <li><code>specifier</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> specifier passed to <code>import()</code></li> <li><code>module</code> <a href="https://nodejs.org/api/vm.html#vm_class_vm_sourcetextmodule" class="type">&lt;vm.SourceTextModule&gt;</a></li> <li>Returns: <a href="https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects" class="type">&lt;Module Namespace Object&gt;</a> | <a href="https://nodejs.org/api/vm.html#vm_class_vm_sourcetextmodule" class="type">&lt;vm.SourceTextModule&gt;</a> Returning a <code>vm.SourceTextModule</code> is recommended in order to take advantage of error tracking, and to avoid issues with namespaces that contain <code>then</code> function exports.</li> </ul> </li> </ul> </li> </ul> <p>If <code>options</code> is a string, then it specifies the filename.</p> <p>Creating a new <code>vm.Script</code> object compiles <code>code</code> but does not run it. The compiled <code>vm.Script</code> can be run later multiple times. The <code>code</code> is not bound to any global object; rather, it is bound before each run, just for that run.</p> <h3>script.createCachedData()<span><a class="mark" href="#vm_script_createcacheddata" id="vm_script_createcacheddata">#</a></span></h3> <div class="api_metadata"> <span>Added in: v10.6.0</span> </div> <ul> <li>Returns: <a href="https://nodejs.org/api/buffer.html#buffer_class_buffer" class="type">&lt;Buffer&gt;</a></li> </ul> <p>Creates a code cache that can be used with the Script constructor's <code>cachedData</code> option. Returns a Buffer. This method may be called at any time and any number of times.</p> <pre><code class="language-js">const script = new vm.Script(` function add(a, b) { return a + b; } const x = add(1, 2); `); const cacheWithoutX = script.createCachedData(); script.runInThisContext(); const cacheWithX = script.createCachedData(); </code></pre> <h3>script.runInContext(contextifiedSandbox[, options])<a class="srclink" href="https://github.com/nodejs/node/blob/3a2e75d9a5c31d20e429d505b82dd182e33f459a/lib/vm.js#L130">[src]</a><span><a class="mark" href="#vm_script_runincontext_contextifiedsandbox_options" id="vm_script_runincontext_contextifiedsandbox_options">#</a></span></h3> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v6.3.0</td> <td><p>The <code>breakOnSigint</code> option is supported now.</p></td></tr> <tr><td>v0.3.1</td> <td><p><span>Added in: v0.3.1</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>contextifiedSandbox</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> A <a href="#vm_what_does_it_mean_to_contextify_an_object">contextified</a> object as returned by the <code>vm.createContext()</code> method.</li> <li> <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a></p> <ul> <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a> When <code>true</code>, if an <a href="https://nodejs.org/api/errors.html#errors_class_error"><code>Error</code></a> occurs while compiling the <code>code</code>, the line of code causing the error is attached to the stack trace. <strong>Default:</strong> <code>true</code>.</li> <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;integer&gt;</a> Specifies the number of milliseconds to execute <code>code</code> before terminating execution. If execution is terminated, an <a href="https://nodejs.org/api/errors.html#errors_class_error"><code>Error</code></a> will be thrown. This value must be a strictly positive integer.</li> <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a> If <code>true</code>, the execution will be terminated when <code>SIGINT</code> (Ctrl+C) is received. Existing handlers for the event that have been attached via <code>process.on('SIGINT')</code> will be disabled during script execution, but will continue to work after that. If execution is terminated, an <a href="https://nodejs.org/api/errors.html#errors_class_error"><code>Error</code></a> will be thrown. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&lt;any&gt;</a> the result of the very last statement executed in the script.</li> </ul> <p>Runs the compiled code contained by the <code>vm.Script</code> object within the given <code>contextifiedSandbox</code> and returns the result. Running code does not have access to local scope.</p> <p>The following example compiles code that increments a global variable, sets the value of another global variable, then execute the code multiple times. The globals are contained in the <code>sandbox</code> object.</p> <pre><code class="language-js">const util = require('util'); const vm = require('vm'); const sandbox = { animal: 'cat', count: 2 }; const script = new vm.Script('count += 1; name = "kitty";'); const context = vm.createContext(sandbox); for (let i = 0; i &lt; 10; ++i) { script.runInContext(context); } console.log(util.inspect(sandbox)); // { animal: 'cat', count: 12, name: 'kitty' } </code></pre> <p>Using the <code>timeout</code> or <code>breakOnSigint</code> options will result in new event loops and corresponding threads being started, which have a non-zero performance overhead.</p> <h3>script.runInNewContext([sandbox[, options]])<a class="srclink" href="https://github.com/nodejs/node/blob/3a2e75d9a5c31d20e429d505b82dd182e33f459a/lib/vm.js#L141">[src]</a><span><a class="mark" href="#vm_script_runinnewcontext_sandbox_options" id="vm_script_runinnewcontext_sandbox_options">#</a></span></h3> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v10.0.0</td> <td><p>The <code>contextCodeGeneration</code> option is supported now.</p></td></tr> <tr><td>v6.3.0</td> <td><p>The <code>breakOnSigint</code> option is supported now.</p></td></tr> <tr><td>v0.3.1</td> <td><p><span>Added in: v0.3.1</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>sandbox</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> An object that will be <a href="#vm_what_does_it_mean_to_contextify_an_object">contextified</a>. If <code>undefined</code>, a new object will be created.</li> <li> <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a></p> <ul> <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a> When <code>true</code>, if an <a href="https://nodejs.org/api/errors.html#errors_class_error"><code>Error</code></a> occurs while compiling the <code>code</code>, the line of code causing the error is attached to the stack trace. <strong>Default:</strong> <code>true</code>.</li> <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;integer&gt;</a> Specifies the number of milliseconds to execute <code>code</code> before terminating execution. If execution is terminated, an <a href="https://nodejs.org/api/errors.html#errors_class_error"><code>Error</code></a> will be thrown. This value must be a strictly positive integer.</li> <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a> If <code>true</code>, the execution will be terminated when <code>SIGINT</code> (Ctrl+C) is received. Existing handlers for the event that have been attached via <code>process.on('SIGINT')</code> will be disabled during script execution, but will continue to work after that. If execution is terminated, an <a href="https://nodejs.org/api/errors.html#errors_class_error"><code>Error</code></a> will be thrown. <strong>Default:</strong> <code>false</code>.</li> <li><code>contextName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> Human-readable name of the newly created context. <strong>Default:</strong> <code>'VM Context i'</code>, where <code>i</code> is an ascending numerical index of the created context.</li> <li><code>contextOrigin</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> <a href="https://developer.mozilla.org/en-US/docs/Glossary/Origin">Origin</a> corresponding to the newly created context for display purposes. The origin should be formatted like a URL, but with only the scheme, host, and port (if necessary), like the value of the <a href="https://nodejs.org/api/url.html#url_url_origin"><code>url.origin</code></a> property of a <a href="https://nodejs.org/api/url.html#url_class_url"><code>URL</code></a> object. Most notably, this string should omit the trailing slash, as that denotes a path. <strong>Default:</strong> <code>''</code>.</li> <li> <p><code>contextCodeGeneration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a></p> <ul> <li><code>strings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a> If set to false any calls to <code>eval</code> or function constructors (<code>Function</code>, <code>GeneratorFunction</code>, etc) will throw an <code>EvalError</code>. <strong>Default:</strong> <code>true</code>.</li> <li><code>wasm</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a> If set to false any attempt to compile a WebAssembly module will throw a <code>WebAssembly.CompileError</code>. <strong>Default:</strong> <code>true</code>.</li> </ul> </li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&lt;any&gt;</a> the result of the very last statement executed in the script.</li> </ul> <p>First contextifies the given <code>sandbox</code>, runs the compiled code contained by the <code>vm.Script</code> object within the created sandbox, and returns the result. Running code does not have access to local scope.</p> <p>The following example compiles code that sets a global variable, then executes the code multiple times in different contexts. The globals are set on and contained within each individual <code>sandbox</code>.</p> <pre><code class="language-js">const util = require('util'); const vm = require('vm'); const script = new vm.Script('globalVar = "set"'); const sandboxes = [{}, {}, {}]; sandboxes.forEach((sandbox) =&gt; { script.runInNewContext(sandbox); }); console.log(util.inspect(sandboxes)); // [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }] </code></pre> <h3>script.runInThisContext([options])<a class="srclink" href="https://github.com/nodejs/node/blob/3a2e75d9a5c31d20e429d505b82dd182e33f459a/lib/vm.js#L121">[src]</a><span><a class="mark" href="#vm_script_runinthiscontext_options" id="vm_script_runinthiscontext_options">#</a></span></h3> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v6.3.0</td> <td><p>The <code>breakOnSigint</code> option is supported now.</p></td></tr> <tr><td>v0.3.1</td> <td><p><span>Added in: v0.3.1</span></p></td></tr> </tbody></table> </details> </div> <ul> <li> <p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a></p> <ul> <li><code>displayErrors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a> When <code>true</code>, if an <a href="https://nodejs.org/api/errors.html#errors_class_error"><code>Error</code></a> occurs while compiling the <code>code</code>, the line of code causing the error is attached to the stack trace. <strong>Default:</strong> <code>true</code>.</li> <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;integer&gt;</a> Specifies the number of milliseconds to execute <code>code</code> before terminating execution. If execution is terminated, an <a href="https://nodejs.org/api/errors.html#errors_class_error"><code>Error</code></a> will be thrown. This value must be a strictly positive integer.</li> <li><code>breakOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a> If <code>true</code>, the execution will be terminated when <code>SIGINT</code> (Ctrl+C) is received. Existing handlers for the event that have been attached via <code>process.on('SIGINT')</code> will be disabled during script execution, but will continue to work after that. If execution is terminated, an <a href="https://nodejs.org/api/errors.html#errors_class_error"><code>Error</code></a> will be thrown. <strong>Default:</strong> <code>false</code>.</li> </ul> </li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&lt;any&gt;</a> the result of the very last statement executed in the script.</li> </ul> <p>Runs the compiled code contained by the <code>vm.Script</code> within the context of the current <code>global</code> object. Running code does not have access to local scope, but <em>does</em> have access to the current <code>global</code> object.</p> <p>The following example compiles code that increments a <code>global</code> variable then executes that code multiple times:</p> <pre><code class="language-js">const vm = require('vm'); global.globalVar = 0; const script = new vm.Script('globalVar += 1', { filename: 'myfile.vm' }); for (let i = 0; i &lt; 1000; ++i) { script.runInThisContext(); } console.log(globalVar); // 1000 </code></pre> <h2>Class: vm.SourceTextModule<span><a class="mark" href="#vm_class_vm_sourcetextmodule" id="vm_class_vm_sourcetextmodule">#</a></span></h2> <div class="api_metadata"> <span>Added in: v9.6.0</span> </div> <p></p><div class="api_stability api_stability_1"><a href="https://nodejs.org/api/documentation.html#documentation_stability_index">Stability: 1</a> - Experimental</div><p></p> <p><em>This feature is only available with the <code>--experimental-vm-modules</code> command flag enabled.</em></p> <p>The <code>vm.SourceTextModule</code> class provides a low-level interface for using ECMAScript modules in VM contexts. It is the counterpart of the <code>vm.Script</code> class that closely mirrors <a href="https://tc39.github.io/ecma262/#sec-source-text-module-records">Source Text Module Record</a>s as defined in the ECMAScript specification.</p> <p>Unlike <code>vm.Script</code> however, every <code>vm.SourceTextModule</code> object is bound to a context from its creation. Operations on <code>vm.SourceTextModule</code> objects are intrinsically asynchronous, in contrast with the synchronous nature of <code>vm.Script</code> objects. With the help of async functions, however, manipulating <code>vm.SourceTextModule</code> objects is fairly straightforward.</p> <p>Using a <code>vm.SourceTextModule</code> object requires four distinct steps: creation/parsing, linking, instantiation, and evaluation. These four steps are illustrated in the following example.</p> <p>This implementation lies at a lower level than the <a href="https://nodejs.org/api/esm.html#esm_ecmascript_modules">ECMAScript Module loader</a>. There is also currently no way to interact with the Loader, though support is planned.</p> <pre><code class="language-js">const vm = require('vm'); const contextifiedSandbox = vm.createContext({ secret: 42 }); (async () =&gt; { // Step 1 // // Create a Module by constructing a new `vm.SourceTextModule` object. This // parses the provided source text, throwing a `SyntaxError` if anything goes // wrong. By default, a Module is created in the top context. But here, we // specify `contextifiedSandbox` as the context this Module belongs to. // // Here, we attempt to obtain the default export from the module "foo", and // put it into local binding "secret". const bar = new vm.SourceTextModule(` import s from 'foo'; s; `, { context: contextifiedSandbox }); // Step 2 // // "Link" the imported dependencies of this Module to it. // // The provided linking callback (the "linker") accepts two arguments: the // parent module (`bar` in this case) and the string that is the specifier of // the imported module. The callback is expected to return a Module that // corresponds to the provided specifier, with certain requirements documented // in `module.link()`. // // If linking has not started for the returned Module, the same linker // callback will be called on the returned Module. // // Even top-level Modules without dependencies must be explicitly linked. The // callback provided would never be called, however. // // The link() method returns a Promise that will be resolved when all the // Promises returned by the linker resolve. // // Note: This is a contrived example in that the linker function creates a new // "foo" module every time it is called. In a full-fledged module system, a // cache would probably be used to avoid duplicated modules. async function linker(specifier, referencingModule) { if (specifier === 'foo') { return new vm.SourceTextModule(` // The "secret" variable refers to the global variable we added to // "contextifiedSandbox" when creating the context. expo