Nodejs spawn script.exe ENOENT



我在电子应用程序中运行exe时出错。路径是正确的,但仍然存在错误。

Uncaught Error: spawn exe/0c8c86d42f4a8d77842972cdde6eb634.exe ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:267)
at onErrorNT (internal/child_process.js:469)
at processTicksAndRejections (internal/process/task_queues.js:84)

这是我的代码:

const executable = execFile(
'script.exe',
parameter,
{
cwd: 'D:/My Data/VS Code/ReactJS/FrontEND/CrownAIO/src/app/components/task/Scripts',
},
(error: any, stdout: any, stderr: any) => {
if (error) {
console.error('stderr', stderr);
throw error;
}
console.log('stdout', stdout);
}
);

execFile为什么抛出错误?如何在NodeJS中适当地运行带有args的可执行文件?

将一个可执行文件添加到文件script.exe中,那么希望您的parametervar是一个数组。请参阅nodejs文档https://nodejs.org/dist/latest-v14.x/docs/api/child_process.html#child_process_child_process_execfile_file_args_options_callback

例如,如果我使用execFile方法运行bash脚本,我会执行以下操作:

execFile('bash', ['test.sh'], { cwd: '.' }, 
(err, stdout, stderr) => {
console.log(stdout)
}
)

另一个例子,运行nodejs程序

execFile('node', ['test.js'], { cwd: '.' }, 
(err, stdout, stderr) => {
console.log(stdout)
}
)

请注意,这里的cwd选项是无用的,因为它在默认情况下是这样设置的。

最新更新