Run exe file with Child Process NodeJS
Asked Answered
G

1

8

I want to open google chrome with nodejs, but I get this error (I used execFile and spawn),

code

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)
Geraldgeralda answered 20/5, 2014 at 15:43 Comment(6)
I think, you're missing a : in the file path. I think it should be C:\\Program Files\\Google\\Chrome\\Application\\chrome.exeStreetcar
I fixed it with start iexploreGeraldgeralda
But now I have a problem with starting linux command, the same issue. spawn('google-chrome')Geraldgeralda
Can you paste an example for linux pleaseGeraldgeralda
This is the strange's thing, I create a json file with all the commands in mac, and in windows and linux, those strings are the somehow corrupted. changed it in the linux and windows machines, and now it works. why?Geraldgeralda
For future visitors that may want reference about this error: #27689304Savoirfaire
D
4

Each command is executed in a separate shell, so the first cd only affects that shell process which then terminates. If you want to run git in a particular directory, just have Node set the path for you:

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

cwd (current working directory) is one of many options available for exec.refer to link

Dourine answered 10/1, 2015 at 10:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.