Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js – events js 72 throw er unhandled 'error' event

Tags:

node.js

I'm new to Node.js and wish to run a program using streams. With other programs, I had to start a server simultaneously (mongodb, redis, etc) but I have no idea if I'm supposed to run one with this. Please let me know where I am going wrong and how I can rectify this. Thanks in advance.

This is the program:

var http = require('http'),
feed = 'http://isaacs.iriscouch.com/registry/_changes?feed=continuous';


function decide(cb) {
setTimeout(function () {
if (Date.now()%2) { return console.log('rejected'); }        
cb();
}, 2000);
}

http.get(feed, function (res) {

decide(res.pipe.bind(res, process.stdout));


//using anonymous function instead of bind:
// decide(function () {
//   res.pipe(process.stdout)
// });

});

This is the cmd output:

<b>C:\05-Employing Streams\05-Employing Streams\23-Playing with pipes>node npm_stre
am_piper.js

events.js:72
throw er; // Unhandled 'error' event
          ^
Error: Parse Error
at Socket.socketOnData (http.js:1583:20)
at TCP.onread (net.js:527:27)
</b>
like image 483
user3514893 Avatar asked Apr 09 '14 11:04

user3514893


4 Answers

Close nodejs app running in another shell. Restart the terminal and run the program again.


Another server might be also using the same port that you have used for nodejs. Kill the process that is using nodejs port and run the app.

To find the PID of the application that is using port:8000

$ fuser 8000/tcp
8000/tcp:            16708

Here PID is 16708 Now kill the process using the kill [PID] command

$ kill 16708
like image 150
Ganesh Pandey Avatar answered Nov 06 '22 11:11

Ganesh Pandey


I had the same problem. I closed terminal and restarted node. This worked for me.

like image 25
Vu Luu Avatar answered Nov 06 '22 12:11

Vu Luu


Well, your script throws an error and you just need to catch it (and/or prevent it from happening). I had the same error, for me it was an already used port (EADDRINUSE).

like image 3
kungfooman Avatar answered Nov 06 '22 12:11

kungfooman


I always do the following whenever I get such error:

// remove node_modules/
rm -rf node_modules/
// install node_modules/ again
npm install // or, yarn

and then start the project

npm start //or, yarn start

It works fine after re-installing node_modules. But I don't know if it's good practice.

like image 2
Bhojendra Rauniyar Avatar answered Nov 06 '22 11:11

Bhojendra Rauniyar