How to get default export out of vm.SourceTextModule?

Viewed 265

Trying the new SourceTextModule API, I'm trying to extract my exports.

Here's what I've got:


async function loader() {
    // ...

    const context = vm.createContext({
        require: filename => {
            return filename;
        },
        module: {
            exports: {},
        }
    });
    const mod = new vm.SourceTextModule(source, {
        context,
        identifier: module.identifier(),
    });

    async function linker(specifier, referencingModule) {
        throw new Error(`Unable to resolve dependency: ${specifier}`);
    }

    await mod.link(linker);

    const result = await mod.evaluate();

    console.log("RESULT", result);
}

Where source is:

var manifest = {
  name: "MyApp",
  // ...
};
export default manifest;

This is printing

RESULT [Object: null prototype] { result: undefined }

How can I get access to that export default? i.e., I want that manifest object.

1 Answers
Related