Node child_process.spawn multiple commands

From Node.js v6 you can specify a shell option in spawn method which will run command using shell and thus it is possible to chain commands using spawn method.

For example this:

var spawn = require('child_process').spawn;
var child = spawn('ls && ls && ls', {
  shell: true
});
child.stderr.on('data', function (data) {
  console.error("STDERR:", data.toString());
});
child.stdout.on('data', function (data) {
  console.log("STDOUT:", data.toString());
});
child.on('exit', function (exitCode) {
  console.log("Child exited with code: " + exitCode);
});

Will trigger an error on node.js version less than 6:

Error: spawn ls && ls && ls ENOENT

But on version 6 and higher it will return expected result:

node app.js
STDOUT: app.js

STDOUT: app.js
app.js

Child exited with code: 0

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)