Skip to content

SyntheticModule

[<Interface>]
type SyntheticModule =
    static member Create (setExport: Action<string,obj>, error: obj, identifier: string, context: Context, namespace: Object, status: ModuleStatus, evaluate: ModuleEvaluateOptions option -> Promise<unit>, link: ModuleLinker -> Promise<unit>) : SyntheticModule
    abstract member setExport (name: string, value: obj) : unit
AssemblyXantham.Fable.Node
ImplementsModule
Attributes[<Interface>]

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...

@since v13.0.0, v12.16.0

Methods

static member Create (setExport: Action<string,obj>, error: obj, identifier: string, context: Context, namespace: Object, status: ModuleStatus, evaluate: ModuleEvaluateOptions option -> Promise<unit>, link: ModuleLinker -> Promise<unit>) : SyntheticModule
abstract member setExport (name: string, value: obj) : unit

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.

import vm from 'node:vm';

const m = new vm.SyntheticModule(['x'], () => {
m.setExport('x', 1);
});

await m.link(() => {});
await m.evaluate();

assert.strictEqual(m.namespace.x, 1);

@since v13.0.0, v12.16.0

Parameter
nameName of the export to set.
valueThe value to set the export to.