我不知道如何exe
在node.js
. 这是我正在使用的代码。它不工作,不打印任何东西。有没有可能exe
使用命令行执行文件的方法?
var fun = function() {
console.log("rrrr");
exec('CALL hai.exe', function(err, data) {
console.log(err)
console.log(data.toString());
});
}
fun();
我不知道如何exe
在node.js
. 这是我正在使用的代码。它不工作,不打印任何东西。有没有可能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 或任何支持 Node JS(React 或 Vue)的前端框架
const { execFile } = require('child_process');
const child = execFile('chrome.exe', [], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
如果.exe文件位于机器中的某个位置,请将chrome.exe替换为您要执行的应用程序的路径,例如"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
const child = execFile('C:\Program Files (x86)\Google\Chrome\Application\chrome.exe', [], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
你有没有想过在这个过程中使用批处理文件?我的意思是使用 node.js 启动一个 .bat 文件,它会同时启动一个 .exe 文件?
只是在顶部使用答案我得到了这个:
在 exe 文件的目录中创建一个 .bat 文件
输入bat文件
START <full file name like E:\\Your folder\\Your file.exe>
输入您的 .js 文件:
const shell = require('shelljs')
shell.exec('E:\\Your folder\\Your bat file.bat')