使用子进程 NodeJS 运行 exe 文件

pup*_*701 7 windows child-process node.js

我想用 nodejs 打开谷歌浏览器,但出现此错误(我使用了 execFile 和 spawn),

代码

var execFile = require('child_process').execFile,
spawn = require('child_process').spawn,

spawn('C\\Program Files\\Google\\Chrome\\Application\\chrome.exe', function (error, stdout, stderr) {
   if (error !== null) { console.log('exec error: ' + error); }
});

events.js:72
    throw er; // Unhandled 'error' event
          ^
Error: spawn ENOENT
at errnoException (child_process.js:998:11)
at Process.ChildProcess._handle.onexit (child_process.js:789:34)
Run Code Online (Sandbox Code Playgroud)

xde*_*akv 4

每个命令都在单独的 shell 中执行,因此第一个 cd 仅影响该 shell 进程,然后该进程终止。如果你想在特定目录中运行 git,只需让 Node 为你设置路径:

exec('git status', {cwd: '/home/ubuntu/distro'}, /* ... */);
Run Code Online (Sandbox Code Playgroud)

cwd(当前工作目录)是 exec 可用的众多选项之一。参考链接