在 golang 项目/文件中执行节点脚本

发布于 2025-01-12 22:15:55 字数 409 浏览 2 评论 0原文

我正在尝试在我的 golang 项目中执行或运行节点脚本。我正在使用 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)

这是我得到的响应:

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

不确定我可能错了什么,或者是否有其他方法来执行此命令。我发现的只是执行 .sh 命令的方法,

我们将非常感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

背叛残局 2025-01-19 22:15:56

我通过使用命令和文件的绝对路径解决了这个问题,这里是一个工作代码示例:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文