Question

I'm building a system which uses spawn to run commands as specified by an array that's read in from a file. Everything is working fine when I use test commands (like echo Something), however when I set the command npm install express -save the stdout is what you get if you enter npm without any arguments.

Here' the code processing the commands:

var runProcess = function (process, buildDir, callback) {
  console.log('Running command: ' + process);
  var args = process.split(" ");
  var command = args[0];
  var proc;
  var stdout;
  var stderr;

  // Set arguments by shifting array
  args.shift();

  // Spawn the command
  if (args.length) {
    if (command === 'npm') {
      proc = spawn(command, [args, "--color", "always"], {
        cwd: buildDir
      });
    } else {
      proc = spawn(command, [args], {
        cwd: buildDir
      });
    }
  } else {
    proc = spawn(command, [], {
      cwd: buildDir
    });
  }
}

If at any point I console.log to check if it's getting into the right place it traces correctly, but the output is always just the npm commands dump with the usage information as if it's not reading the arguments.

Was it helpful?

Solution

You are getting your types mixed up as @maxorq suggests. You want a single array containing only strings. You are nesting arrays. Try this:

  proc = spawn(command, args.concat( "--color", "always"), {
    cwd: buildDir
  });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top