VM
new vm.Script()
Compiles a fixed script for later execution. Reusing compiled code does not share state unless contexts are shared. Compilation is not validation for hostile code. Tested on Node.js 22.16.0. Example output (one line per entry): ["6 10"].
Formal Syntax & Structure
nodejs syntaxnodejs
new vm.Script(code[, options])Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| code | string | Reviewed source. |
| options | object | string | Compilation options or filename. |
Return Value
Script
Compiled script.
Working Example
Example Snippetnodejs
import { Script } from 'node:vm';const script = new Script('quantity * 2');console.log(script.runInNewContext({ quantity: 3 }, { timeout: 100 }), script.runInNewContext({ quantity: 5 }, { timeout: 100 }));