Spawn on Node JS (Windows Server 2012)

node.js, spawn, windows, windows-server-2012

Solution

(Firstly, does `ls` actually exist on windows?)

I had a similar issue spawning child processes a little while back and it took me ages to figure out the correct way of doing it.

Here's some example code:

var spawn = require('child_process').spawn;
var cp = spawn(process.env.comspec, ['/c', 'command', '-arg1', '-arg2']);
 
cp.stdout.on("data", function(data) {
    console.log(data.toString());
});
 
cp.stderr.on("data", function(data) {
    console.error(data.toString());
});

Have a look at this ticket for an explanation of the issue: https://github.com/joyent/node/issues/2318

[Updated 25/12/2022] As per Gorky's answer, just set `shell: true`

Problem

When I run this through Node: ``` var spawn = require('child_process').spawn; ls = spawn('ls', ['C:\\Users']); ls.on('error', function (err) { console.log('ls error', err); }); ls.stdout.on('data', function (data) { console.log('stdout: ' + data); }); ls.stderr.on('data', function (data) { console.log('stderr: ' + data); }); ls.on('close', function (code) { console.log('child process exited with code ' + code); }); ``` I get the following error: ``` ls error { [Error: spawn ENOENT] code: 'ENOENT', errno: 'ENOENT', syscall: 'spawn' } child process exited with code -1 ``` On Windows Server 2012. Any ideas?

Original source