2

I need to call to CMD command from my node JS application , is it possible ?

I try with the following (POC) and I got error

var express = require('express');
var app = express();

app.get('/', function (req, res) {
    function cmd_exec(cmd, args, cb_stdout, cb_end) {
        var spawn = require('child_process').spawn,
            child = spawn(cmd, args),
            me = this;
        me.exit = 0;  // Send a cb to set 1 when cmd exits
        child.stdout.on('data', function (data) {
            cb_stdout(me, data)
        });
        child.stdout.on('end', function () {
            cb_end(me)
        });
    }
    foo = new cmd_exec('npm', 'install glob --save',
        function (me, data) {
            me.stdout += data.toString();
        },
        function (me) {
            me.exit = 1;
        }
    );

    setTimeout(
        // wait 0.25 seconds and print the output
        log_console,
        250);


    function log_console() {
        console.log(foo.stdout);
    }
    res.send("Hello world");
});

I saw this code in the following link

node.js shell command execution

The error is : TypeError: Incorrect value of args option in line child = spawn(cmd, args),

what am I doing wrong here ?Currnlty I just use the npm install command(just for testing) but any other command that I can execute and run will be sufficient

3
  • 1
    clearly args contains something it shouldn't. It looks like you gave it a function... Commented Jul 27, 2015 at 15:14
  • 1
    The command in this case is node, the arguments are install glob --save Commented Jul 27, 2015 at 15:16
  • @KevinB - can You please provide example what I should change as answer and I'll try it ASAP,Thanks! Commented Jul 27, 2015 at 15:23

1 Answer 1

2

When executing a terminal command, there are two parts: The command, and the arguments. In your case, the command is npm, and the arguments is everything that comes after that.

cmd_exec('npm', ['install', 'glob', '--save'],
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks I've tried it and I got the same error ,any idea why?Since for first I run it as poc I've just one file which is called server.js and I run it,I've update my question with all the code in the server.js file(this is all my program)
oops, supposed to be an array.
Thanks 1+ Kevin Now the error is changed to following, any idea what it can be? events.js:85 throw er; // Unhandled 'error' event ^ Error: spawn npm ENOENT at exports._errnoException (util.js:746:11) at Process.ChildProcess._handle.onexit (child_process.js:1053:32) at child_process.js:1144:20 at process._tickCallback (node.js:355:11)
No idea. looks unreleated to me.
I provided in the post all the code which i use in the poc ,ive just one file this exact the same code that I provided and I try to test it...does it run for you ?can you suggest other command which I can use to verify that this is working (other then the npm insatll...) thanks in advance!
|

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.