Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get default export out of vm.SourceTextModule?

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.

like image 775
mpen Avatar asked Sep 14 '25 23:09

mpen


1 Answers

You can use mod.namespace....

like image 161
akvadrako Avatar answered Sep 16 '25 12:09

akvadrako