24

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?

amiry jd
  • 27,021
  • 30
  • 116
  • 215
FredTheWebGuy
  • 2,546
  • 3
  • 27
  • 34
  • possible duplicate of [node.js shell command execution](http://stackoverflow.com/questions/14458508/node-js-shell-command-execution) – Mike Pennington Apr 10 '13 at 07:48
  • Possible duplicate of [Get the output of a shell command in node.js](https://stackoverflow.com/questions/12941083/get-the-output-of-a-shell-command-in-node-js) – Damjan Pavlica Jul 17 '18 at 12:55

3 Answers3

16

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.

Shimon Rachlenko
  • 5,469
  • 40
  • 51
11

You could also try the node-cmd package:

const nodeCmd = require('node-cmd');
nodeCmd.get('dir', (err, data, stderr) => console.log(data));

On newer versions of the package, the syntax changed a little:

const nodeCmd = require('node-cmd');
nodeCmd.run('dir', (err, data, stderr) => console.log(data));
Haroldo_OK
  • 6,612
  • 3
  • 43
  • 80
  • 1
    This resolved the brick wall I was hitting! Thanks :) – Adam Marsh Jan 04 '21 at 15:11
  • 2
    get function is not defined in newer version. New exmaple can be found here https://www.npmjs.com/package/node-cmd – AmirHossein Rezaei Apr 19 '22 at 04:41
  • 1
    The node-cmd module internally uses child_process only => (https://github.com/RIAEvangelist/node-cmd) – Devanand Sharma Nov 25 '22 at 10:09
  • can i use this with ```await```and promises ? Thank you – Ahmed Can Unbay Jan 26 '23 at 11:00
  • Not sure if if supports Promised directly, but even if it doesn't, you can always wrap it inside a promise; if `err` is non-falsy, you would call `reject(err)`; otherwise, you'd call `resolve(data)`. Please see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise in order to see how to use `resolve` and `reject` to implement a Promise. – Haroldo_OK Jan 27 '23 at 10:08
3

I know this question is old, but it helped me get to my solution using promises. Also see: this question & answer

const util = require('util');
const exec = util.promisify(require('child_process').exec);

async function runCommand(command) {
  const { stdout, stderr, error } = await exec(command);
  if(stderr){console.error('stderr:', stderr);}
  if(error){console.error('error:', error);}
  return stdout;
}


async function myFunction () {
    // your code here building the command you wish to execute ...
    const command = 'dir';
    const result = await runCommand(command);
    console.log("_result", result);
    // your code here processing the result ...
}

// just calling myFunction() here so it runs when the file is loaded
myFunction();
Adam Marsh
  • 976
  • 11
  • 21