26

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?

2

3 Answers 3

19

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.

Sign up to request clarification or add additional context in comments.

Comments

13

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));

It does not support Promises directly, but you can easily wrap them inside one:

const nodeCmd = require('node-cmd');
const promise = new Promise((resolve, reject) => {
    nodeCmd.run('dir', (err, data, stderr) => {
        if (err) {
            reject(err);
        } else {
            resolve(data, stderr);
        }
    });
});

5 Comments

This resolved the brick wall I was hitting! Thanks :)
get function is not defined in newer version. New exmaple can be found here npmjs.com/package/node-cmd
The node-cmd module internally uses child_process only => (github.com/RIAEvangelist/node-cmd)
can i use this with awaitand promises ? Thank you
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 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… in order to see how to use resolve and reject to implement a Promise.
5

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.