简体   繁体   English

运行某些命令时 Electron NodeJS 生成错误:spawn ENOENT

[英]Electron NodeJS spawn error when running certain commands: spawn ENOENT

So, I'm trying to use spawn in my Electron application to run the flutter --version command.因此,我尝试在我的 Electron 应用程序中使用spawn来运行flutter --version命令。 This command works perfectly fine on my terminal.这个命令在我的终端上工作得很好。

Every solution I've seen implies I don't have it in my process.env.PATH variable.我见过的每个解决方案都暗示我的process.env.PATH变量中没有它。 But when I do a console.log( process.env.PATH );但是当我做一个console.log( process.env.PATH ); , the path to my Flutter command is there. ,我的 Flutter 命令的路径就在那里。

Here is the current code I'm trying to execute:这是我正在尝试执行的当前代码:

const flutterVer = spawn('flutter', ['--version', '/c']);

flutterVer.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

Which returns:哪个返回:

Uncaught Error: spawn flutter ENOENT
    at __node_internal_captureLargerStackTrace (node:internal/errors:464:5)
    at __node_internal_errnoException (node:internal/errors:594:12)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:282:19)
    at onErrorNT (node:internal/child_process:477:16)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

I tried testing with another command, like git --version , and that worked perfectly fine.我尝试使用另一个命令进行测试,例如git --version ,并且效果很好。

I've found a neat workaround by using exec instead of spawn like so.我通过使用exec而不是spawn找到了一个巧妙的解决方法。

const { exec } = require('child_process');

const flutterVer = exec('flutter --version');

flutterVer.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

Will have too look into what the differences are between exec and spawn , but for now, it does what I need.也会研究execspawn之间的区别,但就目前而言,它可以满足我的需要。

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

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