NodeJS on Ubuntu 20.04 - Cannot start server because address in use (:::80) but I can't find any port listening to that address - node.js

When running the following command:
sudo node server/server.js
I receive the following error:
Listening on port 80 events.js:174
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::80
at Server.setupListenHandle [as _listen2] (net.js:1280:14)
at listenInCluster (net.js:1328:12)
at Server.listen (net.js:1415:7)
at Function.listen (/home/app/node_modules/express/lib/application.js:618:24
However when looking at similar questions I was advised to find the process using the port and end it.
When running on root and regular user the following command:
lsof -n -i:80
as well as
netstat -tulpn | grep :80
I get 0 results and no output returned.
Killing the node daemon (using pkill node) and then restarting it didn't work either.
Changing the port gives the same error strangely:
Error: listen EADDRINUSE: address already in use :::8080
Why am I still getting this error?
EDIT The Server Code:
// optional: allow environment to specify port
const port = process.env.PORT || 80;
// wire up the module
const express = require("express");
var http = require("http");
var request = require("request");
// create server instance
const app = express();
// bind the request to an absolute path or relative to the CWD
app.use(express.static(path.join(__dirname, "../dist")));
app.use(express.json());
app.listen(port, () => {
console.log(`This app is listening at http://localhost:${port}`);
});
...

Hi can you upload the server.js content .
You have to run your http server in a other port or try to stop nginx or Apache2 service if you installed them

Simply Kill the Process::
sudo kill $(sudo lsof -t -i:8080)
where replace 8080 with your address and then rerun your app.
If you want to know all the port or address in use simply install nmap from ubuntu snap store and scan all the port using terminal with command
nmap localhost

Related

Address is already in use after killing ports

I've killed this port 8080 a lot of times with npx kill-port 8080
but I still get an error saying:
Error: listen EADDRINUSE: address already in use :::8080
Here is the code
const app = express()
app.post("/dblwebhook", webhook.listener(async vote => {
console.log(`${vote.user} has voted me`)
}))
app.listen(8080)
Am I killing the port wrong? (This is on my centos 8 vps)

listen EADDRINUSE: address already in use -> No matter what port I use?

NodeJS version: LTS 12.17 installed like this:
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt install nodejs
I have a pretty weird issue:
One of my NodeJS app suddenly reports: listen EADDRINUSE: address already in use and it doesn't help anything no matter what port # I try to change it to?
The full trace-stack looks like this:
Error: listen EADDRINUSE: address already in use 0.0.0.0:6080
at Server.setupListenHandle [as _listen2] (net.js:1313:16)
at listenInCluster (net.js:1361:12)
at doListen (net.js:1498:7)
at processTicksAndRejections (internal/process/task_queues.js:85:21)
I'm listening like this (_wl = a Winston logger instance):
> _server.listen(_port, '0.0.0.0', function () { _wl.info('SERVER STARTED! (listening on port # ' + _port + ')')});
I have just setup this AWS EC2 Ubuntu 20.04 instance - so I guess somehow it has something to do with this. It has been working for years on a lot of different instance earlier Windows (I guess I have never run it on Ubuntu earlier).
I execute like like:
1. cd into folder
2. node ./server.js (I have also tried to use Sudo)
Can it have something to do with permissions?
I have tried to allow all in /out going traffic in the server atteched Security Group.
I have check that the firewall in Ubuntu 20.04 is disabled as well.
Also, on the same server I'm running a Python app which exposes a web socket server and the NodeJS app has no issue to subscribe to this connection...
And yes - I have tried to check all ports in use at the server and only few ports are in use.
The NodeJS app also try to expose a websocket server at a given port - but no matter what port I try to use I get the error above.
Sorry!
I use express in my NodeJS apps and most of my apps contain the following line:
app.listen(port); // In this case port # 6080
Later on in the current app (in which I experience the issue described above) I initialize a web socket server as well like this:
const _server = require('http').createServer();
_server.on('request', app);
_server.listen(_port, '0.0.0.0', function () { _wl.info('WEB SOCKET SERVER STARTED! (listening on port # ' + _port + ')')});
in the code above both port and _port used the same ENV variable/setting(6080). Do to my app/Express in this case actually didn't use the app.listen(port) to anything and that Windows just choose to overrule the port's use-case when the web socket server afterwards got initialized with the same port number --> is the reason why I never have experienced any issues on Windows. But Linux/Ubuntu is more sensitive in this regard.
Cheers! :-)

Node js port is already in use

When I start my webserver, node throws listen EADDRINUSE: address already in use :::3000, I tried to use netstat and search for 3000 port (or another, it happens with any port), but nothing found. Also no node processes in task manager, no webpage on localhost:3000. Also I tried to reload windows, but nothing changed.
From your code you are trying to bind the socket module to port 3000:
var io = require('socket.io')(3000);
/* ... */
app.listen(3000);
in this way when the server tries to bind itself the port is already in use.
You have to create the HTTP server and then bind the socket.io module on it:
const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
io.on('connection', () => { /* … */ });
server.listen(3000);
(taken from the socket.io documentation)
Assuming you are on Windows 10, I would recommend trying Resource Monitor. The Network tab has a view called Listening Ports, which should tell you which process is using port 3000
Resource Monitor
It is because of the port is busy to other tasks,You have to stop them with following commands first command shows you process id that is running on port you can stop this process with second command.
$ sudo netstat -nltp | grep
$ sudo kill

I currently run my amazon ec2 instance on port 3000. I want to run it on port 80 instead. How can I do it?

I have a node.js app that runs on port 3000. I deployed it on amazon web services (ec2) and it works over there. My server.js file says:
var port = process.env.PORT || 3000;
(...)
app.listen(port);
console.log('App listening on port ' + port);
My security group in aws settings seems to have the port 80 also opened:
so I thought it's enough to just change the var port to = 80 and restart the server. But when I did that I got an error:
bitnami#ip-172-31-47-102:~/apps/myproject/www/myproject$ sudo node server.js
App listening on port 80
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::80
at Object.exports._errnoException (util.js:856:11)
at exports._exceptionWithHostPort (util.js:879:20)
at Server._listen2 (net.js:1237:14)
at listen (net.js:1273:10)
at Server.listen (net.js:1369:5)
at Function.app.listen (/opt/bitnami/apps/myproject/www/myproject/node_modules/express/lib/application.js:542:24)
at Object.<anonymous> (/opt/bitnami/apps/myproject/www/myproject/server.js:43:5)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:430:10)
at startup (node.js:141:18)
at node.js:1003:3
I'm using the Bitnami MEAN 3.2.1-0 system on Amazon.
Also, the reason why I want to change this port is this:
so far all my webservices operate on port 3000. However, I also have there a public_html folder with the index.html file. So when any user wants to display my webpage he has to enter not only the webpage, but also the port (3000) which is not that convenient.
So far the whole app stays under www.ec2-some-random-amazom-numbers.eu-west-1.compute.amazonaws.com:3000/index.html so I will buy a normal top level domain to point at it (eg. something.com ) but then - do I still need to change the port 3000 to 80 in that case? Or maybe it's common to leave apps on port other than 80?
If the latter, then will it be possible for me to leave the port as it is and just point the top level domain on this whole long amazon one with a port 3000 at the end?
So for example: when user types www.something.com it will redirect him to
www.ec2-some-random-amazom-numbers.eu-west-1.compute.amazonaws.com:3000/index.html ?
Something like this should work for you:
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
You want to use iptables to forward request coming in on port 80 to port 3000 internally.
Based on the error you're getting, (EADDRINUSE), some other web server is listening on port 80 already on your server. If you can prevent that server from running, then you can change your app to run on port 80.
Do the following:
Figure out what's already running on port 80 on your server. There's a good chance it's Apache. Try using netstat to verify.
Kill it (and prevent it from restarting). This will depend on whats listening.
Move your app to port 80 just like you've already tried.
Additional resources:
http://www.cyberciti.biz/faq/what-process-has-open-linux-port/
If you are using the Bitnami Mean Stack then Apache is listening in port 80, hence the conflict. You can either stop the bundled Apache:
sudo /opt/bitnami/ctlscript.sh stop apache
Or you could add a ProxyPass rule in the Apache configuration:
https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypass
I use nginx proxy server for port forwarding
I have faced the same issue.
The solution is to start your NodeJs or ExpressJs app using Port 80, I mean
var port = 80 and start the app using sudo node bin/www
My issue is not able to access the app from internet when the app is running on port 3000: NodeJs App in AWS using port 3000 is not accessible from Internet

Im getting Listen EACCES on my nodejs app

Im new in linux, so I got a aws amazon server and install nodejs and mongodb in var/www/html .
Dir:
-var
-www
-html
-server.js
-public
-node_modules
-express
-mongodb
-mongoose
In my server.js:
var express = require('express');
var app = express();
app.use(express.static(__dirname + "/public"));
app.listen(8080);
When I run:
$ node server
I get this error:
Error: listen EADDRINUSE
at errnoException (net.js:905:11)
at Server._listen2 (net.js:1043:14)
at listen (net.js:1065:10)
at Server.listen (net.js:1139:5)
at EventEmitter.listen (/var/www/html/node_modules/express/lib/application.j s:617:24)
My app is running in port 8080.But I cant run mongosee with this error. Can u help me guys?
Looks like the port 8080 is already in use. Try to set different port to your program temporarily:
app.listen(8085);
If the problem disappear you need to find which program is using port 8080. You can use netstat -tulpn for such task.

Resources