Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Execute Windows Shell Commands (Cmd.exe) with Node JS

I would like to

C:\>ACommandThatGetsData > save.txt

But instead of parsing and saving the data in the console, I would like to do the above command with Node.JS

How to execute a shell command with Node.JS?

like image 723
FredTheWebGuy Avatar asked Apr 10 '13 07:04

FredTheWebGuy


People also ask

How do I run a shell command in 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. If we use the spawn() module, its output will be available via event listeners.

How do I run a program from CMD EXE?

Type "start [filename.exe]" into Command Prompt, replacing "filename" with the name of your selected file. Replace "[filename.exe]" with your program's name. This allows you to run your program from the file path.


1 Answers

Use process.execPath():

process.execPath('/path/to/executable');

Update

I should have read the documentations better.

There is a Child Process Module which allows to execute a child process. You will need either child_process.exec, child_process.execFile or child_process.spawn. All of these are similar in use, but each has its own advantages. Which of them to use depends on your needs.

like image 184
Shimon Rachlenko Avatar answered Sep 18 '22 15:09

Shimon Rachlenko