Comprehensive Guide to the Node.js VM Module (node:vm)

The node:vm module allows you to compile and execute JavaScript code inside isolated V8 contexts — essentially creating lightweight sandboxes within your Node.js application. These contexts have their own global scope and can run code independently from the main environment. However, vm is NOT a security sandbox. It is powerful for dynamic code execution, template engines, REPLs, plugin systems, and controlled module execution, but it must never be used to run untrusted code.

VM Contextvm.runInContextvm.runInNewContextvm.runInThisContextvm.Script

~3 min read · Updated Dec 30, 2025

1. Introduction


The node:vm module provides APIs for compiling and running JavaScript code inside isolated V8 contexts. Each context has its own globalThis, allowing code to run independently from the main application environment. This makes vm ideal for dynamic evaluation, plugin systems, template engines, and REPL implementations.


2. Accessing the Module


const vm = require('node:vm');

3. Core Concepts: Contexts & Isolation


Node.js executes JavaScript inside a V8 Context. The vm module allows you to create additional contexts, each with its own global scope. Code executed inside a context only sees the variables and objects explicitly provided to it.


4. Creating a Context


const context = { foo: 'hello', console };
vm.createContext(context);

Now context becomes the global object for any code executed inside it.


5. Running Code in Different Ways


5.1 vm.runInContext()


Runs code inside an existing context.

vm.runInContext('foo += " world!"; console.log(foo)', context);
console.log(context.foo); // "hello world!"

5.2 vm.runInNewContext()


Creates a new context (or uses a provided sandbox) and runs code inside it.

const result = vm.runInNewContext('a + b', { a: 10, b: 20 });
console.log(result); // 30

5.3 vm.runInThisContext()


Runs code in the current global context — with full access to Node.js internals. Use with extreme caution.

vm.runInThisContext('process.exit(1)');

5.4 vm.Script — Precompiled Code


Compile once, run many times for better performance.

const script = new vm.Script('count += 1; name = "kitty"');

const context = { count: 0 };
vm.createContext(context);

for (let i = 0; i < 1000; i++) {
  script.runInContext(context);
}

console.log(context.count); // 1000

6. Useful Options


{
  filename: 'my-script.vm',
  lineOffset: 0,
  columnOffset: 0,
  timeout: 1000,
  breakOnSigint: true,
  microtaskMode: 'afterEvaluate'
}

7. Module Support (Experimental)


Requires --experimental-vm-modules.

const module = new vm.SourceTextModule('export default 42');
await module.link(() => {});
await module.evaluate();
console.log(module.namespace.default); // 42

8. Dynamic import() Support


Supported via importModuleDynamically callback or USE_MAIN_CONTEXT_DEFAULT_LOADER.


9. Security Warning


vm is NOT a secure sandbox. Never run untrusted code with it.

Reasons:

  • May access Node.js internals if exposed
  • Can freeze the event loop
  • Prototype manipulation can escape sandbox
  • No true isolation

Never do this:

vm.runInNewContext(userCode, { require, process });

Safer Alternatives

  • Use vm.constants.DONT_CONTEXTIFY
  • Freeze or strictly limit the sandbox
  • Use timeout
  • For real isolation: child_process, Worker Threads, containers

10. Practical Examples


10.1 Simple Sandbox


const sandbox = {
  result: null,
  console: { log: (msg) => console.log('User:', msg) }
};
vm.createContext(sandbox);

vm.runInContext('result = 2 + 3 * 4', sandbox);
console.log(sandbox.result); // 14

10.2 Template Engine


function render(template, data) {
  const code = `with(data) { return \`${template}\`; }`;
  const fn = new vm.Script(code).runInNewContext({ data });
  return fn;
}

console.log(render('Hello {{name}}!', { name: 'Ali' }));

10.3 Measuring Memory


vm.measureMemory({ mode: 'detailed', execution: 'eager' })
  .then(info => console.log(info));

11. Best Practices


  • Always use createContext() + runInContext().
  • Never expose require, process, or global.
  • Use timeout for potentially unsafe code.
  • Use vm.Script for repeated execution.
  • Use microtaskMode: 'afterEvaluate' for async code.

Conclusion


The node:vm module is a powerful tool for dynamic JavaScript execution, plugin systems, template engines, and isolated logic testing. However, it must never be used as a security sandbox. With careful sandbox design and proper limitations, vm enables flexible and controlled execution of dynamic code inside Node.js applications.


Written & researched by Dr. Shahin Siami

Related Articles

Comprehensive Guide to the V8 Module in Node.js (node:v8)

The node:v8 module exposes low-level APIs that interact directly with the V8 JavaScript engine embedded in Node.js. . These APIs provide access to heap statistics, heap snapshots, coverage tools, serialization mechanisms, V8 flags, object queries, and promise lifecycle hooks. The module is essential for performance analysis, memory debugging, tooling, and advanced Node.js internals work.

Continue

Comprehensive Guide to Node.js Worker Threads (node:worker_threads)

The node:worker_threads module enables true multithreading in Node.js by running JavaScript in separate threads. While Node.js is traditionally single‑threaded, worker threads allow CPU‑intensive tasks to run in parallel without blocking the event loop. They support shared memory, zero‑copy transfers, worker pools, resource limits, and advanced synchronization APIs. Worker threads are ideal for heavy computation, data processing, and parallel workloads—while async I/O remains best handled by the main thread.

Continue

Comprehensive Guide to the Node.js util Module (node:util)

The node:util module provides a powerful collection of helper functions used throughout Node.js core and extremely useful for application developers. These utilities support debugging, inspection, formatting, type checking, callback/Promise conversions, argument parsing, text encoding, MIME handling, and more. It is one of the most versatile and essential toolkits in the Node.js ecosystem.

Continue

Comprehensive Guide to the URL Module in Node.js

The node:url module provides tools for parsing, constructing, and manipulating URLs. Node.js supports two URL APIs: WHATWG URL API — modern, browser‑compatible, standards‑based. Legacy Node.js URL API — older, Node‑specific, now discouraged. The WHATWG API is the recommended approach for all modern applications. It provides a clean, consistent interface for working with URL components, query parameters, and structured URL patterns.

Continue

راهنمای جامع UDP / Datagram Sockets در Node.js

ماژول node:dgram پیاده‌سازی کامل سوکت‌های UDP را در Node.js فراهم می‌کند. UDP یک پروتکل سبک، بدون اتصال (connectionless) و مناسب برای برنامه‌های بلادرنگ مانند VoIP، بازی‌ها، IoT، سیستم‌های پخش (broadcast) و چندپخشی (multicast) است. این ماژول امکان ساخت سوکت، ارسال و دریافت دیتاگرام، مدیریت TTL، عضویت در گروه‌های multicast، کنترل بافرها، و مدیریت رفتار سطح پایین شبکه را فراهم می‌کند.

Continue

Comprehensive Guide to Trace Events and TTY in Node.js

Node.js provides two powerful low‑level modules for diagnostics and terminal interaction: node:trace_events — a high‑resolution tracing system that captures internal activity from V8, Node.js core, and userland code. It is essential for profiling, performance analysis, and deep debugging. node:tty — a module that exposes TTY (terminal) interfaces, enabling advanced CLI tools, raw input handling, cursor control, color detection, and terminal resizing. This guide explains both modules in a clean, structured, and practical way.

Continue