'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.
Solution 1:[1]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | O.Dev |
