You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
No, this feature is not available in goja_nodejs at the moment. But it is possible to do it. goja_nodejs is a module and can therefore be used by other programs written in Golang. You can use goja_nodejs in a project and build the project like this:
The Go code goja_nodejs.go:
package main
import (
"os""github.com/dop251/goja""github.com/dop251/goja_nodejs/console""github.com/dop251/goja_nodejs/require"
)
funcmain() {
registry:=new(require.Registry)
runtime:=goja.New()
registry.Enable(runtime)
console.Enable(runtime)
registry.Enable(runtime)
filePath:=os.Args[1] // Take the js file from the process arguments listfile, err:=os.ReadFile(filePath) // Resolve the js fileiferr!=nil {
panic(err)
}
runtime.RunString(string(file)) // Pass the js file content to goja
}
The Javascript code main.js:
console.log("Hello world");
Then build the project:
go build goja_nodejs.go
After that run the exectutable:
./goja_nodejs /home/dev/goja_nodejs_exec/main.js
See the result of execution in the console:
Hello world
You can also run without compiling:
go run goja_nodejs.go /home/dev/goja_nodejs_exec/main.js
It should be noted that because this is for a demonstration the path to the js file must be absolute.
Is there any way to execute node as if it were on the command line
like
The text was updated successfully, but these errors were encountered: