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
contextObjectis an object, thevm.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
trueif the givenobjectobject 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 compilescode, runs it within the context of thecontextifiedObject, then returns the result. - vm.runInNewContext()
This method is a shortcut to
- vm.runInThisContext()
vm.runInThisContext()compilescode, runs it within the context of the currentglobaland returns the result.
Classes 4
- class vm.Module
This feature is only available with the
--experimental-vm-modulescommand flag enabled. - class vm.Script
Instances of the
vm.Scriptclass contain precompiled scripts that can be executed in specific contexts. - class vm.SourceTextModule
This feature is only available with the
--experimental-vm-modulescommand flag enabled. - class vm.SyntheticModule
This feature is only available with the
--experimental-vm-modulescommand flag enabled.
Interfaces 15
- interface BaseOptions
- interface CompileFunctionOptions
- interface Context
- interface CreateContextOptions
- interface MeasureMemoryOptions
- interface MemoryMeasurement
- interface ModuleEvaluateOptions
- interface ModuleRequest
A
ModuleRequestrepresents the request to import a module with given import attributes and phase. - interface RunningCodeInNewContextOptions
- interface RunningCodeOptions
- interface RunningScriptInNewContextOptions
- interface RunningScriptOptions
- interface ScriptOptions
- interface SourceTextModuleOptions
- interface SyntheticModuleOptions
Types 4
Namespaces 1
- namespace vm.constants
Returns an object containing commonly used constants for VM operations.