Skip to content
Tutorials Center
My learning

Topics

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.

NODE
import vm from 'node:vm';

const add = vm.compileFunction('return a + b', ['a', 'b']);
console.log(add(2, 3));

Try it

Node.js Body and parameters

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.

Node.js Contexts, extensions and error names

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

NODE
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; }' as code does not give you that function; it gives you a function whose body is a function expression statement, which returns undefined.
  • 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 parsingContext changes 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 SyntaxError when 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 Function always compiles in the current global scope and gives you no file name or extension objects.