使用Node.js执行一个EXE文件

55
我不知道如何在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();

Windows上,对吧? - Peter Mortensen
5个回答

74

你可以在Node.js中尝试使用child process模块的execFile函数。

参考:child_process.execFile(file[, args][, options][, callback])

你的代码应该类似于:

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,比如我想通过文件夹结构运行imagemagik命令exec('C:\Program Files\ImageMagick-6.9.1-Q16-HDRI/convert.exe convert -version', function(){})。 - Umashankar
1
要将两个参数添加到命令中,例如'/t'和'600',请添加以下代码:exec('HelloJithin.exe', ['/t', '600'], function(err, data) { console.log(err) console.log(data.toString()); }); - CyberProdigy

17
如果您要执行的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;
}

文档


5
如果您正在使用Node.js或任何支持Node.js的前端框架(ReactVue.js),
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);
});

0
你有没有考虑过在这个过程中使用批处理文件?我的意思是使用Node.js启动一个.bat文件,同时启动一个.exe文件。
只需按照上面的答案,我得到了以下内容:
  1. 在exe文件所在的目录中创建一个.bat文件
  2. 在bat文件中输入以下内容:START <完整的文件名,例如E:\\你的文件夹\\你的文件.exe>
  3. 在你的.js文件中输入以下内容: const shell = require('shelljs') shell.exec('E:\\你的文件夹\\你的bat文件.bat')

0

我已经尝试过这个。它有效。

const { exec } = require('child_process');

// Replace 'path/to/your/file.exe' with the actual path to your .exe file
const filePath = 'path/to/your/file.exe';

// Execute the .exe file
exec(filePath, (error, stdout, stderr) => {
  if (error) {
    console.error(`Error executing the .exe file: ${error}`);
    return;
  }

  // Process the output or error messages if needed
  if (stdout) {
    console.log(`Standard output: ${stdout}`);
  }

  if (stderr) {
    console.error(`Standard error: ${stderr}`);
  }
});

你在这里的许多回答,包括一些你为某种原因创建了社区维基页面的回答,似乎很可能是完全或部分由人工智能(例如ChatGPT)编写的。请注意,在这里发布由人工智能生成的内容是不允许的(//meta.stackoverflow.com/q/421831)。如果你在任何回答中使用了人工智能工具进行辅助,请鼓励你将其删除。 - NotTheDr01ds
读者应该仔细和批判性地审查这个回答,因为由人工智能生成的信息往往包含基本错误和错误的信息。如果您发现质量问题和/或有理由相信这个答案是由人工智能生成的,请相应地留下反馈。审查团队需要您的帮助来识别质量问题。 - NotTheDr01ds
试图通过仅使用生成的代码和破碎的英语来混淆它? - Peter Mortensen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接