Node.js vm module, resolvable via both ESM import and CommonJS require.
Implemented as a guest-JavaScript shim.
Isolation is approximate. Unlike Node, a “context” here is not a separate V8 realm.
runInThisContextuses an indirecteval;runInNewContext/runInContextroute free identifiers through awith-scoped sandbox built withnew Function. Inside contextified code,thisandglobalThisboth resolve to the sandbox (property definitions, reflection queries such asReflect.ownKeys(this), and reads all land on the sandbox object), and the contextify marker is invisible to reflection. Intrinsics are still shared with the host realm, however — do not rely onnode:vmas a security boundary; use the filesystem sandbox and process-level isolation instead.
Implemented surface
| Export | Description |
|---|---|
Script(code[, options]) | Compile a script; instances expose runInThisContext, runInContext, runInNewContext, and createCachedData. |
createContext([contextObject]) | Wrap an object as a sandbox usable with runInContext. |
isContext(object) | Whether an object has been contextified. |
compileFunction(code[, params][, options]) | Compile a function body. |
runInThisContext(code[, options]) | Evaluate in the current global scope (indirect eval). |
runInContext(code, context[, options]) | Evaluate against a contextified sandbox. |
runInNewContext(code[, context][, options]) | Evaluate against a fresh sandbox. |
measureMemory([options]) | Returns a resolved memory-measurement result. |
constants | The vm constants object. |
SourceTextModule is present with a minimal status lifecycle only — it does
not link or evaluate ES module imports.
See also
- Node.js upstream: https://nodejs.org/api/vm.html