简体   繁体   English

NodeJS - ChildProcess execFile - 未捕获的错误:spawn ENOENT

[英]NodeJS - ChildProcess execFile - Uncaught Error: spawn ENOENT

I'm trying to run a unix executable as an external application via node.js: ( Reference )我正在尝试通过 node.js 将 unix 可执行文件作为外部应用程序运行:( 参考

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:程序identifiers应该使用参数--help执行,而不是失败:

 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. console.log(execPath)在我的节点项目中打印正确的identifiers执行路径。


This actually returns the directory of the root node project and exits with code 0:这实际上返回了根节点项目的目录并以代码 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?为什么execFile抛出错误?
  • How to appropriately run executables with args in NodeJS?如何在 NodeJS 中正确运行带有 args 的可执行文件?

Thanks to @innis for pointing out that the parameter should be an <Object> :感谢@innis 指出参数应该是<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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM