Node.js throws er Unhandled error event - javascript

I have installed node, npm in my Centos 6 server, and i am using putty for running commands at server.
Node is installed correctly at root and running awesome at anywhere at server.
my project is at /home/shaadise/public_html/Ikon
I have created a hello.js file /home/shaadise/public_html/Ikon
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8080);
console.log('Server started');
while running js:
root#vps [/home/shaadise/public_html/Ikon]# node hello.js
Server started
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:904:11)
at Server._listen2 (net.js:1042:14)
at listen (net.js:1064:10)
at Server.listen (net.js:1138:5)
at Object.<anonymous> (/home/shaadise/public_html/Ikon/hello.js:6:4)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
root#vps [/home/shaadise/public_html/Ikon]# throw er; // Unhandled 'error' event
-bash: throw: command not found
-bash: //: is a directory
Question: where i have to put my node js file and how can i access it????
i tested to run command:
root#vps [/home/shaadise/public_html/Ikon]# netstat -plnt | grep ':8080'
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 27111/nginx

This Error: listen EADDRINUSE categorically means that either you or a daemon is running another application on 8080.
However, to check, try running on a different port?
-edit- as this is getting quite a few upvotes, I thought i'd add a bit of additional debug into it.
Pretty much all node.js tutorials default to port 8080 for running. This is because it is similar to the default port 80 used by other web services, such as Apache or NGinX.
In order to determine if another application is running on the same port, you can use netstat -a to see all active connections and their ports, and then grep that list to find any process connected on the same port as your Node.js application.
It doesn't really matter which port your Node application runs on, as long as it's a free port. Ultimately, when you deploy into production, you would sync up whatever content server you are using (Apache/NGinX) to use the same port.

A common situation to get this error is when one does the following:
startup something
use Ctrl+z and put it into background
try to startup something again
The good way to go would be always try to hit Ctrl+c first which sends signal to the application (which may decide to shutdown).
You can read more about it here:
What is the difference between Ctrl-z and Ctrl-c in the shell?

The server is running in background; it's happing, usually, when you don't kill the process.
To solve this you can put on the terminal:
ps | grep 'node'
This code will show you a process that have a specific number, use the next code to kill the process:
kill -9 "specific number"
You can use sudo if this doesn't work correctly.

If you are using Linux based system, first you have to list all the programs that are using that particular port and kill them(meaning stop them)
exemple: I want to list all programs that are using the 3000 port
fuser 3000/tcp
then pick the process ID, which is in the right side of the obtained line of text and issue the kill command
exemple : if have a process ID with the value of 2345 then the command will be
kill 2345

If closing the process which is using that port doesn't fix it , try the below solutions.
Installing the below package fixed it for me forever.
npm install ws#3.3.2 --save-dev --save-exact
Run this command in your terminal :
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
For Arch Linux add this line to /etc/sysctl.d/99-sysctl.conf:
fs.inotify.max_user_watches=524288
Then execute:
sysctl --system
https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers#the-technical-details

Related

Nodejs Error: listen EADDRINUSE: address already in use :8080

My app was not running but when I try "npm start" command it throws error
Error: listen EADDRINUSE: address already in use :8080
I restart my EC2 instance and try this command again, but I face same error. How do I start my Node server?
Going by your comments, it looks like you have a node process already running your EC2 instance and it's listening on port 8080.
As you have stated lsof -i tcp:8080 gives you,
node 3255 root 20u IPv6 20660 0t0 TCP *:webcache (LISTEN)
The PID of this process is shown in the 2nd column: 3255.
Kill it,
kill -9 3255
After this try running your npm start and it should work

Error: listen EADDRINUSE in nodejs

I've created javascript build system in sublime using that
Then I run server using that code
var server = require('http');
function engine(request, response){
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end("Hello");
}
server.createServer(engine).listen(3000);
but when I'll make change and for example will change Hello to Hello World and refresh browser it doesn't make any sense and when I'm trying rerun (f7) it throws an exception
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::3000
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at Server._listen2 (net.js:1231:14)
at listen (net.js:1267:10)
at Server.listen (net.js:1363:5)
at Object.<anonymous> (C:\Users\gsiradze\Desktop\nodejs\script1.js:4:8)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
[Finished in 0.1s]
I know that it's because of 3000 port is in use but how can I stop that port an then run the new code?
I'm pretty new at node.js, so feel free to give any suggestion
How do you run the nodejs implementation?
You could try to use the package nodemon
npm install -g nodemon
Now you can start your node application by typing nodemon in the directory.
If you change and save your file now, the server will automatically restart your server.
If you dont want to use nodemon, shut down the server with ctrl+c
server.createServer(engine).listen(3000);
that's where you say where port is located, you can change port to any other one.
To rerun your code, I take it you use console you can press ctrl+C to stop process and run it again with node nameOfYoursServerFile.js
Find what is the PID of your process on 3000 with lsof -w -n -i tcp:3000
Then, just kill it with kill -9 PID switching PID with the PID returned by the first command.
You need to get the PID of node service
You can do that by running this command on your terminal
ps aux | grep node
it will give you id of running node process
and the you can use
kill -9 <id of node process>
to stop node process and then try again to run your node server
Either Ctrl+C the foreground process or find your running process's ID by using pidof node and then killing it (kill pid). Even if you are running with forever or in the background some other way, this will work.

How do I get SocketCluster to restart on file change?

I'm running http://socketcluster.io/ and I want to restart my workers whewnever a file changes. However, nodemon server.js fails as soon as it tries to restart with an endlessly-repeating wall of:
1445633138359 - Origin: Worker (PID 44372)
[Error] Error: connect ECONNREFUSED /var/folders/fj/yzfd3_tn7xd0smz7j6s093mh0000gn/T/socketcluster/6879fe94-ed92-4188-b1d7-cb187a5ade4e_b68fcded6c/b0
at Object.exports._errnoException (util.js:874:11)
at exports._exceptionWithHostPort (util.js:897:20)
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
1445633138362 - Worker 0 exited - Exit code: 0
How can I safely restart SocketCluster to load the new changes?
nodemon sends the SIGUSR2 signal to the main (Master) process. SocketCluster (correctly) interprets this as as a request to reboot the workers. Unfortunately, there's an open issue where things are not shut down properly and errors fly all around.
There are two options:
You can add the code from the linked issue:
house.addShutdownHandler(function(ctx, next){
socketCluster.killWorkers();
socketCluster.killBrokers();
next();
});
or use forever to send a "restart everything" signal:
forever -w --killSignal=SIGTERM server.js
Improvements were made for nodemon in SC version 5.0.23 or later.
Make sure that you pass killMasterOnSignal: true when instantiating SocketCluster in your code (server.js file) - This setting is necessary for nodemon to work.

Node js +Error: listen EADDRINUSE + Unhandled 'error' event

I'm using nodeclipse plugin for eclipse to run my node js project.Following js file is working properly but h1 tag is not working .I can only see a plain text.plus I'm getting this exception in the runtime.please help me out.
javascript file
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.end('<html><body><h1>Home</h1> URL was: ' + request.url + '</body></html>');
}).listen(3000, 'localhost');
console.log('Server running at http://localhost:3000/');
exception
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:904:11)
at Server._listen2 (net.js:1042:14)
at listen (net.js:1064:10)
at net.js:1146:9
at dns.js:72:18
at process._tickCallback (node.js:415:13)
at Function.Module.runMain (module.js:499:11)
at startup (node.js:119:16)
at node.js:902:3
Error: listen EADDRINUSE
This error means that you already have another process listening on port 3000.
Here is how to find out which process it is on windows
C:\> netstat -a -b
(add -n to stop it trying to resolve hostnames, which will make it a lot faster)
Edit: +1 for Dane's recommendation for TCPView. Looks very useful!
-a Displays all connections and listening ports.
-b Displays the executable involved in creating each connection or listening port. In some cases well-known executables host multiple independent components, and in these cases the sequence of components involved in creating the connection or listening port is displayed. In this case the executable name is in [] at the bottom, on top is the component it called, and so forth until TCP/IP was reached. Note that this option can be time-consuming and will fail unless you have sufficient permissions. -n Displays addresses and port numbers in numerical form.
As Patrick has said Error: listen EADDRINUSE
This error means that you already have another process listening on port 3000.
If you used Nodeclipse to run Node.js application, you can see list of currently running apps in Debug View (shown by default in Node perspective). Then you can terminate selected or all, restart etc.
Yes, Debug View does not includes only debugged apps. It should have been named Launch View, but it is standard View in Eclipse, we name it as it is named.
Also running apps can be terminated individually by closing its Console (using red square icon)

Node.js app can't run on port 80 even though there's no other process blocking the port

I'm running an instance of Debian on Amazon EC2 with Node.js installed. If I run the code below:
http = require('http');
http.createServer(function (request, response){
response.writeHead(200, {'Content-Type':'text/plain'});
response.end('Hello World\n');
}).listen(80);
console.log("Running server at port 80");
I get the output below which tells me there's another process listening at port 80:
Running server at port 80
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EACCES
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1020:19)
at listen (net.js:1061:10)
at Server.listen (net.js:1127:5)
at Object.<anonymous> (/home/admin/nodetests/nodetest.js:6:4)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
Now when I check to see if there's a process (as root in case anything is hidden) listening on port 80 using:
netstat -tupln
I get the below output, which tells me theres nothing listening at port 80:
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1667/sshd
tcp6 0 0 :::22 :::* LISTEN 1667/sshd
I should note that the debian has port 80 open as an inbound rule if that makes a difference.
My question is: What am I doing wrong? How come I can't identify the process listening to port 80? Why is it blocked in Debian? What steps should I take to get the code running correctly?
The error code EACCES means you don't have proper permissions to run applications on that port. On Linux systems, any port below 1024 requires root access.
Instead of running on port 80 you can redirect port 80 to your application's port (>1024) using
iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3000
This will work if your application is running on port 3000.
Short answer: You can allow node access to that port using:
setcap 'cap_net_bind_service=+ep' /path/to/nodejs
long answer
Edit:
May not work on new node versions
Note that if you have apache running, you can create a reverse proxy on a vhost. If your node is running on port 8080:
<VirtualHost 127.0.0.1:80>
ServerName myLocalServer
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>
Of course, add server to /etc/hosts:
127.0.0.1 myLocalServer
You will need to enable the relevant apache modules:
sudo a2enmod proxy_html
sudo a2enmod proxy_http
sudo a2enmod proxy_connect
sudo a2enmod proxy_ajp
sudo service apache2 restart
...and now you can connect to http://myLocalServer.
For those looking for a quick and easy solution for a development environment, port forwarding via ssh can be a nice alternative:
ssh -L 80:localhost:3000 yourusername#localhost -N
This forwards port 80 on localhost to port 3000 on localhost.
It needs to be run as root (privileged port). To cancel it, simply hit ctrl-c in the terminal. (You can add the -f flag to have the command run in the background, but then you need to find it again to kill it).
This solution requires you to have an ssh server running locally. It can be done quickly, but please bear in mind the security implications if you are on a shared network. You might want to apply at least some level of additional security (disable password & root login).
I personally only ever use this on my local machine. I'm not sure how it affects the processing speed of your requests if you run this on production, maybe someone has an idea. Anyway, you would need to make sure this command keeps running all the time, which introduces more headaches. For production environments, I suggest using a reverse proxy like nginx.
the hexacyanide answer is right. but is there any solution to make this work?
the answer is yes.
how?
you can use a reverse proxy for example run a nginx reverse proxy on port 80 and pass the proxy to destination ip:port that node use it.
you can set this up using docker container that makes life even easier. this is the official build of nginx in docker hub that you can pull it.
there's even more benefits in using reverse proxy that you can google it.
I have got the same error and I tried running my application using sudo and it worked for me.
without sudo
mansi#mansi:~/NodePractice$ node myFirst.js
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EACCES 0.0.0.0:80
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at Server._listen2 (net.js:1224:19)
at listen (net.js:1273:10)
at Server.listen (net.js:1369:5)
at Object.<anonymous> (/home/mansi/NodePractice/myFirst.js:6:4)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
and with sudo
mansi#mansi:~/NodePractice$ sudo node myFirst.js
^C
The error code EACCES means you don't have proper permissions to run applications on that port.
On Linux systems, any port below 1024 requires root access.
Run the program with sudo permision.
Run sudo su command before running the program.

Categories

Resources