Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run exe file with Child Process NodeJS

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)
like image 404
puppeteer701 Avatar asked May 20 '14 15:05

puppeteer701


People also ask

Can we create child process in node JS?

Usually, Node. js allows single-threaded, non-blocking performance but running a single thread in a CPU cannot handle increasing workload hence the child_process module can be used to spawn child processes. The child processes communicate with each other using a built-in messaging system.

How do I run an exec in node JS?

The exec() function in Node. js creates a new shell process and executes a command in that shell. The output of the command is kept in a buffer in memory, which you can accept via a callback function passed into exec() .

Could we run an external process with node js?

Node. js can run shell commands by using the standard child_process module. If we use the exec() function, our command will run and its output will be available to us in a callback.


1 Answers

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

like image 84
xdeepakv Avatar answered Sep 19 '22 09:09

xdeepakv