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
- JavaScript can be compiled and run immediately, or compiled, saved, and run later.
- A contextified object holds the global variables for that context.
See also
Claims
- id: C1 claim: vm compiles and runs code in V8 contexts. source: S1 evidence: "compiling and running code within V8 Virtual Machine contexts"
- id: C2 claim: The node:vm module is not a security mechanism. source: S1 evidence: "The node:vm module is not a security mechanism"
- id: C3 claim: The source docs also warn not to run untrusted code. source: S2 evidence: "Do not use it to run untrusted code"
Sources
- [S1] VM (executing JavaScript). Node.js docs. https://nodejs.org/api/vm.html . License: MIT. Accessed 2026-11-18.
- [S2] vm.md. Node.js source docs. https://raw.githubusercontent.com/nodejs/node/main/doc/api/vm.md . License: MIT. Accessed 2026-11-18.
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
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.},
}
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.