dop251 / goja

ECMAScript/JavaScript engine in pure Go
MIT License
5.43k stars 365 forks source link

Unable to access third-party libraries #547

Closed wwwAngHua closed 9 months ago

wwwAngHua commented 10 months ago

I first ran the axios js library, but I couldn't get the axios object. What happened?

package main

import (
    "fmt"
    "github.com/dop251/goja"
    "io/ioutil"
    "net/http"
)

func main() {
    vm := goja.New()
    axiosSrc, err := ioutil.ReadFile("axios.js")
    if err != nil {
        panic(err)
    }
    _, err = vm.RunString(string(axiosSrc))
    if err != nil {
        panic(err)
    }
    _, err = vm.RunString(`
        axios.get('https://jsonplaceholder.typicode.com/todos/1')
            .then(function (response) {
                console.log(response.data);
            })
            .catch(function (error) {
                console.log(error);
            });
    `)
    if err != nil {
        panic(err)
    }
}

error message:

ReferenceError: axios is not defined at main (:2:5(1))

dop251 commented 10 months ago

Without seeing the contents of axios.js I can only make an educated guess that it did not set globalThis.axios.