Search code examples
javascriptnode.jsexecchild-processspawn

NodeJS - ChildProcess execFile - Uncaught Error: spawn ENOENT


I'm trying to run a unix executable as an external application via node.js: (Reference)

const execFile = require('child_process').execFile;
const executable = execFile('identifiers', ['--help'], [execPath], (error, stdout, stderr) => {
    if (error) {
        console.error('stderr', stderr);
        throw error;
    }
    console.log('stdout', stdout);
});

The program identifiers should be executed with argument --help, instead fails with:

Uncaught Error: spawn identifiers ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:264)
    at onErrorNT (internal/child_process.js:456)
    at processTicksAndRejections (internal/process/task_queues.js:80)

console.log(execPath) prints the correct identifiers exec path, within my node project.


This actually returns the directory of the root node project and exits with code 0:

var sys   = require('sys'),
    spawn = require('child_process').spawn,
    ls    = spawn('ls', ['-l']);

ls.stdout.on('data', function (data) {
    console.log('stdout: ' + data);
});

ls.stderr.on('data', function (data) {
    console.log('stderr: ' + data);
});

ls.on('exit', function (code) {
  console.log('exit code ' + code);
});

  • Why does execFile throw the error?
  • How to appropriately run executables with args in NodeJS?

Solution

  • Thanks to @innis for pointing out that the parameter should be an <Object>:

    const execFile = require('child_process').execFile;
    const executable = execFile('./identifiers', ['--id', '1'], {'cwd': execPath}, (error, stdout, stderr) => {
        if (error) {
            console.error('stderr', stderr);
            throw error;
        }
        console.log('stdout', stdout);
    });