node.js - NodeJS: throw er; //Unhandled 'error' event (events.js:72) when using child_process spawn method -
i've made node.js app list .txt files directory recursively and, each one, stuff.
here's app.js:
var spawn = require('child_process').spawn, dir = spawn('dir', ['*.txt', '/b']); dir.stdout.on('data', function (data) { //do stuff each stdout line... console.log('stdout: ' + data); }); dir.stderr.on('data', function (data) { //throw errors console.log('stderr: ' + data); }); dir.on('close', function (code) { console.log('child process exited code ' + code); }); when run node app.js via console, error message below:
events.js:72 throw er; // unhandled 'error' event ^ error: spawn enoent @ errnoexception (child_process.js:980:11) @ process.childprocess._handle.onexit (child_process.js:771:34) i'm using node v0.10.13 @ win32 environment.
i way (spawn) because want handle stdout line line (the exec method release entire stdout 1 string).
* update *
by way, using
spawnchild_processnot guarantee outputcmd dirline line. i've created question that too.
that happen because dir not executable in windows. it's command shell.
solution problem following:
var dir = spawn('cmd', ['/c', 'dir']); dir.stdout.on("data", function() { // things }) this exact problem here also.
Comments
Post a Comment