user13581602
user13581602

Reputation: 115

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);
});

Upvotes: 2

Views: 2318

Answers (1)

user13581602
user13581602

Reputation: 115

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);
});

Upvotes: 1

Related Questions