Javascript 使用node.js执行exe文件
我不知道如何在Javascript 使用node.js执行exe文件,javascript,node.js,Javascript,Node.js,我不知道如何在node.js中执行exe文件。这是我正在使用的代码。它不工作,也不打印任何内容。是否有任何可能的方法使用命令行执行exe文件 var fun = function() { console.log("rrrr"); exec('CALL hai.exe', function(err, data) { console.log(err) console.log(data.toString()); }); } fun(); 您可以在node.js中尝试子流
node.js
中执行exe
文件。这是我正在使用的代码。它不工作,也不打印任何内容。是否有任何可能的方法使用命令行执行exe
文件
var fun = function() {
console.log("rrrr");
exec('CALL hai.exe', function(err, data) {
console.log(err)
console.log(data.toString());
});
}
fun();
您可以在node.js中尝试子流程模块的execFile函数 参考: 您的代码应该如下所示:
var exec = require('child_process').execFile;
var fun =function(){
console.log("fun() start");
exec('HelloJithin.exe', function(err, data) {
console.log(err)
console.log(data.toString());
});
}
fun();
如果要执行的
exe
位于其他目录中,并且您的exe
与它所在的文件夹有一些依赖关系,请尝试在选项中设置cwd
参数
var exec = require('child_process').execFile;
/**
* Function to execute exe
* @param {string} fileName The name of the executable file to run.
* @param {string[]} params List of string arguments.
* @param {string} path Current working directory of the child process.
*/
function execute(fileName, params, path) {
let promise = new Promise((resolve, reject) => {
exec(fileName, params, { cwd: path }, (err, data) => {
if (err) reject(err);
else resolve(data);
});
});
return promise;
}
您有没有想过在这个过程中使用批处理文件?我的意思是使用node.js启动一个.bat文件,它将同时启动一个.exe文件 仅在顶部使用答案,我就得到了以下结果:
开始
const shell=require('shelljs')
shell.exec('E:\\Your folder\\Your bat file.bat')
如何在某个文件夹中运行exe,比如我想运行imagemagik命令,方法是使用文件夹结构exec('C:\Program Files\ImageMagick-6.9.1-Q16-HDRI/convert.exe convert-version',function(){}),向命令添加两个参数,例如'/t'和'600'add
exec('hellojitin.exe',['/t','600'],function(err,data){console.log)(err)console.log(data.toString());});