Creates a new SyntheticModule instance.
Search the reference...
/
constructor
Array of names that will be exported from the module.
Called when the module is evaluated.
The contextified object as returned by the vm.createContext() method, to compile and evaluate this module in.
String used in stack traces.
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 { SyntheticModule } from 'node:vm';
const source = '{ "a": 1 }';
const syntheticModule = new SyntheticModule(['default'], function() {
const obj = JSON.parse(source);
this.setExport('default', obj);
});
// Use `syntheticModule` in linking
(async () => {
await syntheticModule.link(() => {});
await syntheticModule.evaluate();
console.log('Default export:', syntheticModule.namespace.default);
})();
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.
The value undefined cannot be used for cases where there is not a thrown exception due to possible ambiguity with throw undefined;.
Corresponds to the [[EvaluationError]] field of Cyclic Module Record s in the ECMAScript specification.
The identifier of the current module, as set in the constructor.
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.
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, but module.evaluate() has not yet been called.'evaluating': The module is being evaluated through a module.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 not undefined.
Evaluate the module and its depenendencies. Corresponds to the Evaluate() concrete method field of Cyclic Module Records in the ECMAScript specification.
If the module is a vm.SourceTextModule, evaluate() must be called after the module has been instantiated; otherwise evaluate() will return a rejected promise.
For a vm.SourceTextModule, the promise returned by evaluate() may be fulfilled either synchronously or asynchronously:
If the vm.SourceTextModule has no top-level await in itself or any of its dependencies, the promise will be fulfilled synchronously after the module and all its dependencies have been evaluated.If the evaluation succeeds, the promise will be synchronously resolved to undefined.If the evaluation results in an exception, the promise will be synchronously rejected with the exception that causes the evaluation to fail, which is the same as module.error.If the vm.SourceTextModule has top-level await in itself or any of its dependencies, the promise will be fulfilled asynchronously after the module and all its dependencies have been evaluated.If the evaluation succeeds, the promise will be asynchronously resolved to undefined.If the evaluation results in an exception, the promise will be asynchronously rejected with the exception that causes the evaluation to fail.If the module is a vm.SyntheticModule, evaluate() always returns a promise that fulfills synchronously, see the specification of Evaluate() of a Synthetic Module Record:
If the evaluateCallback passed to its constructor throws an exception synchronously, evaluate() returns a promise that will be synchronously rejected with that exception.If the evaluateCallback does not throw an exception, evaluate() returns a promise that will be synchronously resolved to undefined.The evaluateCallback of a vm.SyntheticModule is executed synchronously within the evaluate() call, and its return value is discarded. This means if evaluateCallback is an asynchronous function, the promise returned by evaluate() will not reflect its asynchronous behavior, and any rejections from an asynchronous evaluateCallback will be lost.
evaluate() could also be called again after the module has already been evaluated, in which case:
If the initial evaluation ended in success (module.status is 'evaluated'), it will do nothing and return a promise that resolves to undefined.If the initial evaluation resulted in an exception (module.status is 'errored'), it will re-reject the exception that the initial evaluation resulted in.This method cannot be called while the module is being evaluated (module.status is 'evaluating').
Fulfills with undefined upon success.
Link module dependencies. This method must be called before evaluation, and can only be called once per module.
Use sourceTextModule.linkRequests(modules) and sourceTextModule.instantiate() to link modules either synchronously or asynchronously.
The function is expected to return a Module object or a Promise that eventually resolves to a Module object. The returned Module must satisfy the following two invariants:
It must belong to the same context as the parent Module.Its status must not be 'errored'.If the returned Module's status is 'unlinked', this method will be recursively called on the returned Module with the same provided linker function.
link() returns a Promise that will either get resolved when all linking instances resolve to a valid Module, or rejected if the linker function either throws an exception or returns an invalid Module.
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.
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);
Name of the export to set.
The value to set the export to.
Resources
ReferenceDocsGuidesDiscordMerch StoreGitHubBlogToolkit
RuntimePackage managerTest runnerBundlerPackage runnerProject
Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicenseBaked with ❤️ in San Francisco
We're hiring →SyntheticModule.constructor constructor | Node.js vm module | Bun,AI智能索引,全网链接索引,智能导航,网页索引
- API documentation for constructor node:vm.SyntheticModule.constructor | Bun