VM

vm.createContext()

Prepares a context with its own global environment. Node VM is not a security sandbox; use only reviewed fixed scripts. Tested on Node.js 22.16.0. Example output (one line per entry): ["true 5"].

Formal Syntax & Structure

nodejs syntaxnodejs
vm.createContext([contextObject[, options]])

Parameters & Attributes

NameTypeDescription
contextObjectobjectInitial context properties.
optionsobjectContext options.

Return Value

object

Contextified object.

Working Example

Example Snippetnodejs
import { createContext, isContext, runInContext } from 'node:vm';const c = createContext({ count: 2 });runInContext('count += 3', c, { timeout: 100 });console.log(isContext(c), c.count);
Node.js Reference