3 Stability: 2 - Unstable. See Caveats, below.
7 You can access this module with:
9 var vm = require('vm');
11 JavaScript code can be compiled and run immediately or compiled, saved, and run later.
15 The `vm` module has many known issues and edge cases. If you run into
16 issues or unexpected behavior, please consult [the open issues on
17 GitHub](https://github.com/joyent/node/search?q=vm+state%3Aopen&type=Issues).
18 Some of the biggest problems are described below.
22 The `sandbox` argument to `vm.runInNewContext` and `vm.createContext`,
23 along with the `initSandbox` argument to `vm.createContext`, do not
24 behave as one might normally expect and their behavior varies
25 between different versions of Node.
27 The key issue to be aware of is that V8 provides no way to directly
28 control the global object used within a context. As a result, while
29 properties of your `sandbox` object will be available in the context,
30 any properties from the `prototype`s of the `sandbox` may not be
31 available. Furthermore, the `this` expression within the global scope
32 of the context evaluates to the empty object (`{}`) instead of to
35 Your sandbox's properties are also not shared directly with the script.
36 Instead, the properties of the sandbox are copied into the context at
37 the beginning of execution, and then after execution, the properties
38 are copied back out in an attempt to propagate any changes.
42 Properties of the global object, like `Array` and `String`, have
43 different values inside of a context. This means that common
44 expressions like `[] instanceof Array` or
45 `Object.getPrototypeOf([]) === Array.prototype` may not produce
46 expected results when used inside of scripts evaluated via the `vm` module.
48 Some of these problems have known workarounds listed in the issues for
49 `vm` on GitHub. for example, `Array.isArray` works around
50 the example problem with `Array`.
52 ## vm.runInThisContext(code, [filename])
54 `vm.runInThisContext()` compiles `code`, runs it and returns the result. Running
55 code does not have access to local scope. `filename` is optional, it's used only
58 Example of using `vm.runInThisContext` and `eval` to run the same code:
64 usingscript = vm.runInThisContext('localVar = 1;',
66 console.log('localVar: ' + localVar + ', usingscript: ' +
68 evaled = eval('localVar = 1;');
69 console.log('localVar: ' + localVar + ', evaled: ' +
72 // localVar: 123, usingscript: 1
73 // localVar: 1, evaled: 1
75 `vm.runInThisContext` does not have access to the local scope, so `localVar` is unchanged.
76 `eval` does have access to the local scope, so `localVar` is changed.
78 In case of syntax error in `code`, `vm.runInThisContext` emits the syntax error to stderr
79 and throws an exception.
82 ## vm.runInNewContext(code, [sandbox], [filename], [timeout])
84 `vm.runInNewContext` compiles `code`, then runs it in `sandbox` and returns the
85 result. Running code does not have access to local scope. The object `sandbox`
86 will be used as the global object for `code`.
87 `sandbox` and `filename` are optional, `filename` is only used in stack traces.
88 `timeout` specifies an optional number of milliseconds to execute `code` before
89 terminating execution. If execution is terminated, `null` will be thrown.
91 Example: compile and execute code that increments a global variable and sets a new one.
92 These globals are contained in the sandbox.
94 var util = require('util'),
101 vm.runInNewContext('count += 1; name = "kitty"', sandbox, 'myfile.vm');
102 console.log(util.inspect(sandbox));
104 // { animal: 'cat', count: 3, name: 'kitty' }
106 Note that running untrusted code is a tricky business requiring great care. To prevent accidental
107 global variable leakage, `vm.runInNewContext` is quite useful, but safely running untrusted code
108 requires a separate process.
110 In case of syntax error in `code`, `vm.runInNewContext` emits the syntax error to stderr
111 and throws an exception.
113 ## vm.runInContext(code, context, [filename], [timeout])
115 `vm.runInContext` compiles `code`, then runs it in `context` and returns the
116 result. A (V8) context comprises a global object, together with a set of
117 built-in objects and functions. Running code does not have access to local scope
118 and the global object held within `context` will be used as the global object
120 `filename` is optional, it's used only in stack traces.
121 `timeout` specifies an optional number of milliseconds to execute `code` before
122 terminating execution. If execution is terminated, `null` will be thrown.
124 Example: compile and execute code in a existing context.
126 var util = require('util'),
132 context = vm.createContext(initSandbox);
134 vm.runInContext('count += 1; name = "CATT"', context, 'myfile.vm');
135 console.log(util.inspect(context));
137 // { animal: 'cat', count: 3, name: 'CATT' }
139 Note that `createContext` will perform a shallow clone of the supplied sandbox object in order to
140 initialize the global object of the freshly constructed context.
142 Note that running untrusted code is a tricky business requiring great care. To prevent accidental
143 global variable leakage, `vm.runInContext` is quite useful, but safely running untrusted code
144 requires a separate process.
146 In case of syntax error in `code`, `vm.runInContext` emits the syntax error to stderr
147 and throws an exception.
149 ## vm.createContext([initSandbox])
151 `vm.createContext` creates a new context which is suitable for use as the 2nd argument of a subsequent
152 call to `vm.runInContext`. A (V8) context comprises a global object together with a set of
153 build-in objects and functions. The optional argument `initSandbox` will be shallow-copied
154 to seed the initial contents of the global object used by the context.
156 ## vm.createScript(code, [filename])
158 `createScript` compiles `code` but does not run it. Instead, it returns a
159 `vm.Script` object representing this compiled code. This script can be run
160 later many times using methods below. The returned script is not bound to any
161 global object. It is bound before each run, just for that run. `filename` is
162 optional, it's only used in stack traces.
164 In case of syntax error in `code`, `createScript` prints the syntax error to stderr
165 and throws an exception.
170 A class for running scripts. Returned by vm.createScript.
172 ### script.runInThisContext([timeout])
174 Similar to `vm.runInThisContext` but a method of a precompiled `Script` object.
175 `script.runInThisContext` runs the code of `script` and returns the result.
176 Running code does not have access to local scope, but does have access to the `global` object
177 (v8: in actual context).
178 `timeout` specifies an optional number of milliseconds to execute `code` before
179 terminating execution. If execution is terminated, `null` will be thrown.
181 Example of using `script.runInThisContext` to compile code once and run it multiple times:
183 var vm = require('vm');
187 var script = vm.createScript('globalVar += 1', 'myfile.vm');
189 for (var i = 0; i < 1000 ; i += 1) {
190 script.runInThisContext();
193 console.log(globalVar);
198 ### script.runInNewContext([sandbox], [timeout])
200 Similar to `vm.runInNewContext` a method of a precompiled `Script` object.
201 `script.runInNewContext` runs the code of `script` with `sandbox` as the global object and returns the result.
202 Running code does not have access to local scope. `sandbox` is optional.
203 `timeout` specifies an optional number of milliseconds to execute `code` before
204 terminating execution. If execution is terminated, `null` will be thrown.
206 Example: compile code that increments a global variable and sets one, then execute this code multiple times.
207 These globals are contained in the sandbox.
209 var util = require('util'),
216 var script = vm.createScript('count += 1; name = "kitty"', 'myfile.vm');
218 for (var i = 0; i < 10 ; i += 1) {
219 script.runInNewContext(sandbox);
222 console.log(util.inspect(sandbox));
224 // { animal: 'cat', count: 12, name: 'kitty' }
226 Note that running untrusted code is a tricky business requiring great care. To prevent accidental
227 global variable leakage, `script.runInNewContext` is quite useful, but safely running untrusted code
228 requires a separate process.