当我通过节点运行它:
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); });
我收到以下错误:
ls error { [Error: spawn ENOENT] code: 'ENOENT',errno: 'ENOENT',syscall: 'spawn' } child process exited with code -1
在Windows Server 2012.任何想法?
解决方法
(首先,ls实际上存在于windows上?)
我有一个类似的问题产生子进程一点点回来,花了我几年时间来找出正确的方法.
以下是一些示例代码:
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()); });
看看这张机票,解释这个问题:https://github.com/joyent/node/issues/2318