Skip to content

Accessing a non-default export from a module eval #41

@Chewie

Description

@Chewie

Hello!

I might just have missed it from the docs, but I can't seem to access functions from evaluated modules that don't do a default export:

vm, _ := qjs.New()
ctx := vm.Context()
foo, _ := ctx.Eval("foo.js", qjs.Code(`
	export default function foo(x) {
		return x * 2;
	}
`), qjs.TypeModule())
// prints "function foo(x)... yadda yadda"
fmt.Printf("foo: %+v\n", foo)


bar, _ := ctx.Eval("bar.js", qjs.Code(`
	export function foo(x) {
		return x * 2;
	}
`), qjs.TypeModule())
// prints "undefined"
fmt.Printf("bar: %+v\n", bar)

My use case is that I receive scripts that are supposed to export a function with a particular name, and I need to call it from go. Is there a way to do this besides writing an extra script that imports from the module and re-exports it as default?

Cheers!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions