Starting inspector on localhost:9229 failed: address already in use - javascript

I'm running a node server using "http.createServer", running on port 3001, when I serve the server the first time it runs, and when I do a small change no matter what I get an error said :
**Starting inspector on localhost:9229 failed: address already in use
Server started on http://localhost:3001
Error: listen EADDRINUSE: address already in use :::3001
at Server.setupListenHandle [as _listen2] (node:net:1372:16)
at listenInCluster (node:net:1420:12)
at Server.listen (node:net:1508:7)**
about the 9229 - tried to investigate it by running
lsof -i :9229
and saw that there is 2 instances of it :
node 88370 33u IPv6 0x**************** 0t0 TCP localhost:9229 (LISTEN)
node 88370 34u IPv4 0x**************** 0t0 TCP localhost:9229 (LISTEN)
when I killed it, it created again and again on each code change, this is the first issue.
the second is regarding: "address already in use :::3001.." what is wrong here? I'm listening to that port -
server.listen(3001);
do you have any ideas where to investigate it maybe?
thanks!
about the second issue i tried to change ports.

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

Node.JS event error

i have just moved server to Amazon EC2.
Node.JS was working fine before but since moved, it gives me an event error which im not really sure whats going on.
i run command netstat -nlp to see if the ports number are opened.
the ports number are opened (8080 and 8181).
its giving an error on line 89:7 which the following:
https.listen(port, function(){
console.log('HTTPS Server listening at port: ' + port);
});
EADDRINUSE means "error - address in use". You need to stop the service already listening on that address (read: IP and port combination). You can discover the pid and name of that service with lsof:
sudo lsof -Pan -i tcp | grep -E '(8080|8181)'
i.e. on my local machine:
$ sudo lsof -Pan -i tcp | grep -E '(51235)'
java 12413 andy 669u IPv4 1088377 0t0 TCP *:51235 (LISTEN)
The pid (process ID) is the second number - in this case, 12413. This can be used to terminate the process.
Use kill 12413 to terminate that process. Another lsof shows there's nothing listening on that port, as it's been killed:
$ sudo lsof -Pan -i tcp | grep -E '(51235)'
<nothing here>
$
It may be that you've already got another service running (HTTP proxies tend to listen on 8080), or possibly another version of your same app. You may have to decide whether to use different ports or move the current service if there's a proxy running there.

Node.js throws er Unhandled error event

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

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