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

Community
  • 1
  • 1
07_05_GuyT
  • 2,787
  • 14
  • 43
  • 88

1 Answers1

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'],
Kevin B
  • 94,570
  • 16
  • 163
  • 180
  • 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) – 07_05_GuyT Jul 27 '15 at 15:33
  • oops, supposed to be an array. – Kevin B Jul 27 '15 at 15:43
  • 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) – 07_05_GuyT Jul 27 '15 at 15:49
  • No idea. looks unreleated to me. – Kevin B Jul 27 '15 at 16:00
  • 1
    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! – 07_05_GuyT Jul 27 '15 at 16:04
  • Ok ,assume that I want to try to set env variable node.myenv via this code what should I change to make it work in cmd_exec('npm', ['install', 'glob', '--save'],,thanks! I need somehow to check that this is working and with the previos command i got the error :(, im fairly new to this topic ... – 07_05_GuyT Jul 27 '15 at 20:52
  • Setting an env var won't affect the currently running node app, if that's what you're trying to do. – Kevin B Jul 27 '15 at 21:01