Node.js Reference ยท node:sqlite and Runtime Services
Node.js vm.compileFunction() method
Reference for vm.compileFunction() in Node.js: compile a function body and parameter names into a real function, with contexts, extensions and error handling.
vm.compileFunction() turns a string holding a function body and a list of parameter names into an ordinary JavaScript function. You do not write the function keyword, the braces or the parameter list inside the string: the body is just the statements, and the parameters are passed separately.
Compared with new Function(...) it gives you more control: you can name the file for stack traces, shift line numbers, add extra variables to the scope, and compile the code for a different vm context. It is the function Node.js itself uses to wrap CommonJS modules.
import vm from 'node:vm'; const add = vm.compileFunction('return a + b', ['a', 'b']); console.log(add(2, 3));
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
vm.compileFunction(code[, params[, options]])
| Name | Description |
|---|---|
code |
A string with the function body. |
params |
An array of parameter name strings. Defaults to no parameters. |
options.filename |
File name shown in stack traces. Default is an empty string. |
options.lineOffset, options.columnOffset |
Added to the reported line and column numbers. Useful when the code was cut out of a larger file. |
options.parsingContext |
A context created by vm.createContext(). Global lookups in the body resolve there. |
options.contextExtensions |
An array of objects. Their properties are visible as variables inside the body. |
options.cachedData |
A Buffer of V8 code-cache data to speed up compilation. |
options.produceCachedData |
If true, cache data is produced for the new function. |
options.importModuleDynamically |
Callback that decides what import() inside the body does. |
Returns: a Function.
Errors: a SyntaxError is thrown at compile time for invalid code. TypeError (code ERR_INVALID_ARG_TYPE) is thrown if code is not a string or params is not an array of strings.
Usage notes
- Only the body. Passing
'function (a) { return a; }'ascodedoes not give you that function; it gives you a function whose body is a function expression statement, which returnsundefined. - Sloppy mode by default. Add
'use strict';as the first statement of the body if you want strict mode. - Not a sandbox. Compiling in a
parsingContextchanges which globals the code sees, but the function is still ordinary code with the same powers as any other. See vm is not a sandbox. - Closures are not captured. The function cannot see local variables of the place where you compiled it; only its parameters, the extensions and the globals of its context.
- Parameters can be any valid identifiers. Invalid names throw a
SyntaxErrorwhen compiling. - Code caching helps most for large bodies that are compiled in many processes; for tiny snippets the cache is not worth the bookkeeping.
- Compared with
new Function():new Functionalways compiles in the current global scope and gives you no file name or extension objects.