【问题标题】:Run exe file with Child Process NodeJS使用子进程 NodeJS 运行 exe 文件
【发布时间】:2014-07-08 23:43:16
【问题描述】:

我想用 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)

【问题讨论】:

  • 我认为,您在文件路径中缺少:。我觉得应该是C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe
  • 我用 start iexplore 修复了它
  • 但是现在我在启动 linux 命令时遇到了问题,同样的问题。 spawn('google-chrome')
  • 你能贴一个linux的例子吗
  • 这很奇怪,我在 mac 中创建了一个包含所有命令的 json 文件,在 windows 和 linux 中,这些字符串以某种方式损坏。在linux和windows机器上改了,现在可以了。为什么?

标签: windows node.js child-process


【解决方案1】:

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

exec('git status', {cwd: '/home/ubuntu/distro'}, /* ... */);

cwd(当前工作目录)是 exec 可用的众多选项之一。refer to link

【讨论】:

    猜你喜欢
    • 2020-05-20
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    相关资源
    最近更新 更多