系统相关
首页 > 系统相关> > 在node-webkit中从JavaScript运行shell命令?

在node-webkit中从JavaScript运行shell命令?

作者:互联网

有没有办法在node-webkit中从JavaScript运行shell命令?
有很多类似的问题,但它对我没有帮助.

我正在尝试构建简单的桌面应用程序以列出已安装的工具.
我已经创建了全局安装的节点模块’tools-v’,当我在命令行中运行它时可以工作.
该模块运行几个命令:npm -v,node -v,git -v等.
我在Windows 7上.

//var sys = require('sys');
var exec = require('child_process').exec;
//var toolsv = (process.platform === "win32" ? "tools-v.cmd" : "tools-v");
$(document).ready(function() {
    //myCmd = "C:\\Users\\win7\\AppData\\Roaming\\npm\\tools-v.cmd";
    //myCmd = toolsv;
    myCmd = 'tools-v';
    //gui.Shell.openItem('firefox',function(error, stdout, stderr) { });
    //opening Firefox works.
    exec(myCmd,  function (error, stdout, stderr) {
        //detached: true;
        console.log('stdout: ' + stdout);
        $('#output').append('stdout: ' + stdout)
        if (error !== null) {
          console.log('exec error: ' + error);
        }
    });
});

我总是得到错误:
“”exec错误:错误:产生ENOENT“”

我尝试了spawn而不是exec.我还在节点模块旁边尝试了其他几个命令.
谢谢.

解决方法:

实际上,这段代码有效.我刚刚没有构建完整的应用程序,我通过sublime build for node-webkit进行了测试.使用grunt进行完整构建解决了每个产生问题.

标签:javascript,shell,node-js,desktop-application,node-webkit
来源: https://codeday.me/bug/20190708/1402921.html