Creates a new SyntheticModule instance.
constructor
vm.SyntheticModule.constructor
Not implemented in Bun
Array of names that will be exported from the module.
Called when the module is evaluated.
Referenced types
interface SyntheticModuleOptions
class SyntheticModule
This feature is only available with the --experimental-vm-modules command flag enabled.
The vm.SyntheticModule class provides the Synthetic Module Record as defined in the WebIDL specification. The purpose of synthetic modules is to provide a generic interface for exposing non-JavaScript sources to ECMAScript module graphs.
import vm from 'node:vm';
const source = '{ "a": 1 }';
const module = new vm.SyntheticModule(['default'], function() {
const obj = JSON.parse(source);
this.setExport('default', obj);
});
// Use `module` in linking...
- error: any
If the
module.statusis'errored', this property contains the exception thrown by the module during evaluation. If the status is anything else, accessing this property will result in a thrown exception.The value
undefinedcannot be used for cases where there is not a thrown exception due to possible ambiguity withthrow undefined;.Corresponds to the
[[EvaluationError]]field of Cyclic Module Record s in the ECMAScript specification. - namespace: Object
The namespace object of the module. This is only available after linking (
module.link()) has completed.Corresponds to the GetModuleNamespace abstract operation in the ECMAScript specification.
- status: ModuleStatus
The current status of the module. Will be one of:
'unlinked':module.link()has not yet been called.'linking':module.link()has been called, but not all Promises returned by the linker function have been resolved yet.'linked': The module has been linked successfully, and all of its dependencies are linked, butmodule.evaluate()has not yet been called.'evaluating': The module is being evaluated through amodule.evaluate()on itself or a parent module.'evaluated': The module has been successfully evaluated.'errored': The module has been evaluated, but an exception was thrown.
Other than
'errored', this status string corresponds to the specification's Cyclic Module Record's[[Status]]field.'errored'corresponds to'evaluated'in the specification, but with[[EvaluationError]]set to a value that is notundefined. - ): Promise<void>;
Evaluate the module.
This must be called after the module has been linked; otherwise it will reject. It could be called also when the module has already been evaluated, in which case it will either do nothing if the initial evaluation ended in success (
module.statusis'evaluated') or it will re-throw the exception that the initial evaluation resulted in (module.statusis'errored').This method cannot be called while the module is being evaluated (
module.statusis'evaluating').Corresponds to the Evaluate() concrete method field of Cyclic Module Record s in the ECMAScript specification.
@returnsFulfills with
undefinedupon success. - link(): Promise<void>;
Link module dependencies. This method must be called before evaluation, and can only be called once per module.
Use
sourceTextModule.linkRequests(modules)andsourceTextModule.instantiate()to link modules either synchronously or asynchronously.The function is expected to return a
Moduleobject or aPromisethat eventually resolves to aModuleobject. The returnedModulemust satisfy the following two invariants:- It must belong to the same context as the parent
Module. - Its
statusmust not be'errored'.
If the returned
Module'sstatusis'unlinked', this method will be recursively called on the returnedModulewith the same providedlinkerfunction.link()returns aPromisethat will either get resolved when all linking instances resolve to a validModule, or rejected if the linker function either throws an exception or returns an invalidModule.The linker function roughly corresponds to the implementation-defined HostResolveImportedModule abstract operation in the ECMAScript specification, with a few key differences:
- The linker function is allowed to be asynchronous while HostResolveImportedModule is synchronous.
The actual HostResolveImportedModule implementation used during module linking is one that returns the modules linked during linking. Since at that point all modules would have been fully linked already, the HostResolveImportedModule implementation is fully synchronous per specification.
Corresponds to the Link() concrete method field of Cyclic Module Record s in the ECMAScript specification.
- It must belong to the same context as the parent
- name: string,value: any): void;
This method sets the module export binding slots with the given value.
import vm from 'node:vm'; const m = new vm.SyntheticModule(['x'], () => { m.setExport('x', 1); }); await m.evaluate(); assert.strictEqual(m.namespace.x, 1);@param nameName of the export to set.
@param valueThe value to set the export to.