1

I am just trying to dive into Node.js and I am testing basic functionalities that I consider helpful to accomplish my project. One of them would be executing a small .exe file I coded in C++ that processes data from text files. I figured out that child_process.execFile might be the best method to achieve that. So I wrote a little script to start a server and invoke an executable. For the first test, I set the path to a "third-party"-.exe and it worked just fine (program is started as expected). However, when I target my own .exe nothing happens at all, although the path is correct (which I countercheck with fs.access). Manually executing the file by mere double-clicking on the .exe also works totally fine and txt-files are accordingly processed. So now I wonder if get sth. fundamentally wrong, e.g. does the .exe need to fulfil a certain condition to be executable with execFile?

Here is my code:

var http = require('http');
const fs = require('fs');

var server = http.createServer(function(req, res){
    console.log('Request was made: ' + req.url);
    res.writeHead(200, {'Content-Type': 'text/plain'});
});

server.listen(3000, '127.0.0.1');
console.log('Listening to port 3000');

var executablePath = "C:/path/to/file.exe";

fs.access(executablePath, fs.constants.F_OK, (err) => {
      console.log(`${executablePath} ${err ? 'does not 
      exist':'exists'}`);
});

const execFile = require('child_process').execFile;
const child = execFile(executablePath, (error, stdout, stderr) => {
    if (error) {
        console.error('stderr', stderr);
        throw error;
    }
    console.log('stdout', stdout);
});

The console output is "...C:/path/to/file.exe exists". execFile does not throw any error. Thx for your help and apologies for my noob language!

emigrand
  • 11
  • 3
  • I'd try to check `fs.access` with `fs.constants.X_OK`, to see whether it is executable. If you want to do the `execFile` after `fs.access`, you might want to put it inside the `fs.access` callback as well. Maybe this can help you debug, I'm not using windows... another wild guess would be to use `exec` or provide the `shell` option to `execFile` – Narigo Dec 19 '18 at 01:59
  • Not sure if I should edit the post or comment, but I found the (noobish) mistake: Paths are not relative to the exe-file, but relative to the index.js with the execFile command. Since the target folders for the exe-file output where not present there, no output was generated at all. Sry for wasting your time but thank you anyway, Narigo - somehow you made me think into the right direction... – emigrand Dec 19 '18 at 19:10

0 Answers0