SyntheticModule

external class SyntheticModule : Module(source)

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.

const vm = require('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...

Since

v13.0.0, v12.16.0

Constructors

Link copied to clipboard
constructor(exportNames: ReadonlyArray<String>, evaluateCallback: () -> Unit, options: SyntheticModuleOptions = definedExternally)

Creates a new SyntheticModule instance.

Properties

Link copied to clipboard
Link copied to clipboard

The specifiers of all dependencies of this module. The returned array is frozen to disallow any changes to it.

Link copied to clipboard
var error: Any?

If the module.status is '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.

Link copied to clipboard

The identifier of the current module, as set in the constructor.

Link copied to clipboard

The namespace object of the module. This is only available after linking (module.link()) has completed.

Link copied to clipboard

The current status of the module. Will be one of:

Functions

Link copied to clipboard
fun evaluate(options: ModuleEvaluateOptions = definedExternally): Promise<Void>

Evaluate the module.

Link copied to clipboard
fun link(linker: ModuleLinker): Promise<Void>

Link module dependencies. This method must be called before evaluation, and can only be called once per module.

Link copied to clipboard
fun setExport(name: String, value: Any?)

This method is used after the module is linked to set the values of exports. If it is called before the module is linked, an ERR_VM_MODULE_STATUS error will be thrown.