NodeJS - ChildProcess execFile - Uncaught Error: spawn ENOENT
NodeJS - ChildProcess execFile - Uncaught Error: spawn ENOENT
我正在尝试通过 node.js 运行 一个 unix 可执行文件作为外部应用程序:(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);
});
程序 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)
在我的节点项目中打印正确的 identifiers
exec 路径。
这实际上是 returns 根节点项目的目录,并以代码 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
抛出错误?
- 如何在 NodeJS 中适当地 运行 带有参数的可执行文件?
感谢@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);
});
我正在尝试通过 node.js 运行 一个 unix 可执行文件作为外部应用程序:(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);
});
程序 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)
在我的节点项目中打印正确的 identifiers
exec 路径。
这实际上是 returns 根节点项目的目录,并以代码 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
抛出错误? - 如何在 NodeJS 中适当地 运行 带有参数的可执行文件?
感谢@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);
});