Skip to main content
SyntheticModule - vm - Node documentation
class SyntheticModule
extends Module
Unstable

Usage in Deno

import { SyntheticModule } from "node:vm";

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

Constructors

new
SyntheticModule(
exportNames: string[],
evaluateCallback: (this: SyntheticModule) => void,
)

Creates a new SyntheticModule instance.

Methods

setExport(
name: string,
value: any,
): void

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);