如何从节点应用程序执行命令

我需要从我的节点JS应用程序调用CMD命令,这可能吗?

我尝试以下(POC),我得到错误

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

我在下面的链接中看到了这个代码

node.js shell命令执行

错误是:TypeError:行中的args选项的值不正确child = spawn(cmd, args),

我在这里做错了什么?Currnlty我只是使用npm install命令(仅用于testing),但我可以执行和运行的任何其他命令

执行terminal命令时,有两部分:命令和参数。 在你的情况下,命令是npm ,参数是在那之后的一切。

 cmd_exec('npm', ['install', 'glob', '--save'],