← All notes

Node.js vm: compile and run code in V8 contexts

Answer: The node:vm module is for compiling and running code within V8 Virtual Machine contexts. It is not a security mechanism. Do not use it to run untrusted code. createContext prepares a context object; runInContext runs code there.

Examples

import { createContext, runInContext } from "node:vm";
const context = { x: 1 };
createContext(context);
runInContext("x += 2", context);
console.log(context.x);

Rules

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Node.js docs, VM (executing JavaScript); Node.js source docs, vm.md. MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Node.js docs, VM (executing JavaScript); Node.js source docs, vm.md. MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors.

Cite this

Permanent ID: iil:note:vhx08tsm

iinventedlight. 2026. "Node.js vm: compile and run code in V8 contexts." iinventedlight Library, iil:note:vhx08tsm, version of 2026-11-18. https://iinventedlight.com/library/notes/node-vm/. License: CC-BY-4.0.

@misc{iil_note_vhx08tsm,
  author = {{iinventedlight}},
  title = {{Node.js vm: compile and run code in V8 contexts}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-18},
  url = {https://iinventedlight.com/library/notes/node-vm/},
  note = {Permanent ID iil:note:vhx08tsm. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/node-vm.md.