Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support shell entry? #86

Open
eric-gitta-moore opened this issue Nov 4, 2024 · 1 comment
Open

Support shell entry? #86

eric-gitta-moore opened this issue Nov 4, 2024 · 1 comment

Comments

@eric-gitta-moore
Copy link

Is there any way to execute node as if it were on the command line

like

goja main.js
@cedrickah
Copy link

cedrickah commented Nov 4, 2024

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"
)

func main() {
    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 list
    file, err := os.ReadFile(filePath) // Resolve the js file
    if err != 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants