[英]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);
});
execFile
throw the error?execFile
抛出错误? 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.