Node.js module

vm

The 'node:vm' module provides APIs to compile and run code within virtual machine contexts. Bun runs this code using JavaScriptCore's VM, which is different from Node.js's V8-based implementation.

The `node:vm` module is not a security mechanism and should not be used to run untrusted code.

Works in Bun · notes

Core script execution functionality works. Experimental VM ES Modules (`vm.Module`, etc.) and the `importModuleDynamically` hook are not implemented. Several options like `timeout`, `breakOnSigint`, and `cachedData` are also not yet implemented.

Functions 7

  • vm.compileFunction()

    Compiles the given code into the provided context (if no context is supplied, the current context is used), and returns it wrapped inside a function with the given `pa…

  • vm.createContext()

    If the given contextObject is an object, the vm.createContext() method will prepare that object and return a reference to it so that it can be used in calls to run…

  • vm.isContext()

    Returns true if the given object object has been contextified using createContext, or if it's the global object of a context created using `vm.constants.DONT_CONTE…

  • vm.measureMemory()

    Measure the memory known to V8 and used by all contexts known to the current V8 isolate, or the main context.

  • vm.runInContext()

    The vm.runInContext() method compiles code, runs it within the context of the contextifiedObject, then returns the result.

  • vm.runInNewContext()

    This method is a shortcut to

  • vm.runInThisContext()

    vm.runInThisContext() compiles code, runs it within the context of the current global and returns the result.

Classes 4

  • class vm.Module

    This feature is only available with the --experimental-vm-modules command flag enabled.

  • class vm.Script

    Instances of the vm.Script class contain precompiled scripts that can be executed in specific contexts.

  • class vm.SourceTextModule

    This feature is only available with the --experimental-vm-modules command flag enabled.

  • class vm.SyntheticModule

    This feature is only available with the --experimental-vm-modules command flag enabled.

Interfaces 15

Types 4

Namespaces 1