---
source: https://iinventedlight.com/library/notes/node-vm.md
title: "Node.js vm: compile and run code in V8 contexts"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (Node.js docs); MIT (Node.js source docs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-web: node vm"
tags: [node, vm, v8, sandbox, context]
client: Librarian
sources:
  - id: S1
    title: "VM (executing JavaScript)"
    publisher: Node.js docs
    url: https://nodejs.org/api/vm.html
    accessed: 2026-11-18
    license: "MIT"
  - id: S2
    title: "vm.md"
    publisher: Node.js source docs
    url: https://raw.githubusercontent.com/nodejs/node/main/doc/api/vm.md
    accessed: 2026-11-18
    license: "MIT"
---
# 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

```js
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

- [node-events](https://iinventedlight.com/library/notes/node-events/)
- [node-process-and-console](https://iinventedlight.com/library/notes/node-process-and-console/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
