O.Dev
O.Dev

Reputation: 51

Executing a node script inside a golang project/file

I'm trying to exec or run a node script inside my golang project. I'm using os/exec

data, err := exec.Command("node api.js", "resources/node/api.js").Output()
    if err != nil {
        panic(err)
    }

    output := string(data)

    fmt.Println(output)

this it the response I'm getting:

panic: exec: "node api.js": executable file not found in %PATH%

Not sure What I might be wrong or if there is an alternative way to execute this command. All I found was ways to execute .sh command

Your assistance will be greatly appreciated.

Upvotes: 0

Views: 1143

Answers (1)

O.Dev
O.Dev

Reputation: 51

I resolved it by using absolute paths for both the command and the file, here is a working code sample:

data, err := exec.Command("C:\\Program Files\\nodejs\\node", "C:\\Users\\Administrator\\Desktop\\project\\resources\\node\\api.js").Output()
    if err != nil {
        panic(err)
    }

    output := string(data)

    fmt.Println(output)

Upvotes: 2

Related Questions