Skip to content

Support for ES Modules #99

Description

@robfig

It looks like v8go does not support ES Modules. For example, I translated the Context example to use modules:

package main

import (
	"fmt"

	"rogchap.com/v8go"
)

func main() {
	ctx, _ := v8go.NewContext()
	val, err := ctx.RunScript("export function add(a, b) { return a + b };", "math.mjs")
	fmt.Println(val, err)
	val, err = ctx.RunScript("import { add } from 'math.mjs';\nconst result = add(3, 4)", "main.js")
	fmt.Println(val, err)
	val, err = ctx.RunScript("result", "value.js")
	fmt.Println(val, err)
	fmt.Println(val)
}

and it resulted in these errors:

<nil> SyntaxError: Unexpected token 'export'
<nil> SyntaxError: Cannot use import statement outside a module
...

I do not typically develop JS or use Node.js, so I might be doing something dumb too.

Do you have any particular ideas around how this might work? I'd be happy to submit a pull request if so.

Thanks!
Rob

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions