PM2 listening on port 443 shows EADDRINUSE: address already in use :::443 - node.js

I have a Node/Express server running on an AWS Lightsail instance with PM2 as a process manager. The server is currently listening on port 4000. The IP address for the instance is attached to a subdomain that has a valid SSL certificate and automatically redirects from HTTP to HTTPS. Visiting https://example.com at the moment shows the 'Congratulations! You are now running Bitnami Node.js 12.18.3 in the Cloud." page.
Currently, all the Express endpoints are only accessible through http://example.com:4000/endpoint, but I want the Express app to run on port 443 so that the endpoints are accessible immediately on https://example.com/endpoint.
I read that PM2 is able to listen on ports 80 and 443 and tried the method mentioned in the documentation, but whenever I change the port number in the .env file to 443 and reload the app using pm2 reload app, I get the following error:
0|app | Error: listen EADDRINUSE: address already in use :::443
0|app | at Server.setupListenHandle [as _listen2] (net.js:1313:16)
0|app | at listenInCluster (net.js:1361:12)
0|app | at Server.listen (net.js:1447:7)
0|app | at Function.listen (/opt/bitnami/apache/htdocs/node_modules/express/lib/application.js:618:24)
0|app | at Object.<anonymous> (/opt/bitnami/apache/htdocs/app.js:44:5)
0|app | at Module._compile (internal/modules/cjs/loader.js:1137:30)
0|app | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
0|app | at Module.load (internal/modules/cjs/loader.js:985:32)
0|app | at Function.Module._load (internal/modules/cjs/loader.js:878:14)
0|app | at Object.<anonymous> (/opt/bitnami/node/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23) {
0|app | code: 'EADDRINUSE',
0|app | errno: 'EADDRINUSE',
0|app | syscall: 'listen',
0|app | address: '::',
0|app | port: 443
0|app | }
App.js
const express = require('express');
const dotenv = require('dotenv');
const app = express();
app.use(express.json()); // for parsing POST bodies
dotenv.config();
app.get("/hello", (req, res) => res.send("Hello World!"));
app.listen(process.env.PORT, () => {
console.log(`🥁 App listening on port ${process.env.PORT}!`);
});
.env
PORT=443
Edit:
pm2 status output

Unfortunately I wasn't able to use Nginx, but instead used Apache's virtual hosts config to redirect all traffic from ports 443 to 4000. More info here.

you console error stats port is already being used, give a try changing PORT=8282 and see if this solves, however, if the problem still persists then move your dotenv configuration at the top require('dotenv').config().

Related

Getting Error while connecting mongodb to Node.js app using mongoose atlas

I was trying to connect the MongoDB atlas using mongoose but this is the error I'm getting.
[nodemon] starting `node app.js`
node:events:505
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (node:net:1372:16)
at listenInCluster (node:net:1420:12)
at Server.listen (node:net:1508:7)
at Function.listen (E:\S and P Global Notes_lect\Nodetest\node_modules\express\lib\application.js:635:24)
at E:\S and P Global Notes_lect\Nodetest\app.js:11:97
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1399:8)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'EADDRINUSE',
errno: -4091,
syscall: 'listen',
address: '::',
port: 3000
}
[nodemon] app crashed - waiting for file changes before starting...
First I thought the error was in the password encoding but I solved it then I got this one
You are most likely already running the server in the same port (3000).
Check your processes and terminate the other instance of the node app that is listening on 3000
Check your network to see if port 3000 is already in use by another application
or
Change the port of your node app to listen to another unused port and the error should go away.

permision denied on port 3000 on nodejs

when i run my nodejs project on port 3000, get following error:
Error: listen EACCES: permission denied :::3000
at Server.setupListenHandle [as _listen2] (node:net:1330:16)
at listenInCluster (node:net:1378:12)
at Server.listen (node:net:1465:7)
at Function.listen (F:\Projects\NodeJS\Nodejs start\Nodejs start\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (F:\Projects\NodeJS\Nodejs start\Nodejs start\app.js:34:5)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1357:8)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'EACCES',
errno: -4092,
syscall: 'listen',
address: '::',
port: 3000
Not only on port 3000, but also on any port, the program does not run, and the mentioned error occur.
for solving this problem, i run net stop winnat on CMD, i get:
The Windows NAT Driver service is not started.
for start the winnat, i run net start winnat , i get
The Windows NAT Driver service was start successfully.
But the problem was not solved.Is there any other way but to restart the system?
When reading an environment variable, you should convert it to a number.
Try adding parseInt() to the variable:
const port = parseInt(process.env.PORT) || 3000;
You can try On Windows systems, restarting the service "Host Network
Service" may resolve the issue.
or try to restart your computer more than one time
or try open CMD under admin rights and run :
net stop winnat
net start winnat

Listening to Port 1880 fails on Windows

I am not able to start a server listening to port 1880. I tried this simple code in Node.js:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello World!');
res.end();
}).listen(1880);
Which gives me the following error:
Error: listen EACCES: permission denied 0.0.0.0:1880
[90m at Server.setupListenHandle [as _listen2] (net.js:1283:19)[39m
[90m at listenInCluster (net.js:1348:12)[39m
[90m at Server.listen (net.js:1436:7)[39m
at Object.<anonymous> (C:\Users\mmueller\Desktop\test\test.js:6:4)
[90m at Module._compile (internal/modules/cjs/loader.js:956:30)[39m
[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)[39m
[90m at Module.load (internal/modules/cjs/loader.js:812:32)[39m
[90m at Function.Module._load (internal/modules/cjs/loader.js:724:14)[39m
[90m at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)[39m
[90m at internal/main/run_main_module.js:17:11[39m
Emitted 'error' event on Server instance at:
[90m at emitErrorNT (net.js:1327:8)[39m
[90m at processTicksAndRejections (internal/process/task_queues.js:80:21)[39m {
code: [32m'EACCES'[39m,
errno: [32m'EACCES'[39m,
syscall: [32m'listen'[39m,
address: [32m'0.0.0.0'[39m,
port: [33m1880[39m
}
What I tried:
Checked if there is another program listening on this port (which is
not the case)
Executed command in admin console
Disabled windows firewall
Disabled Kaspersky tools
Still no success.
With port 1881 it also doesn't work. So some port-range has no access.
But with e.g. port 18800 it works.
What else can I do to figure out why I don't have access to listen to port 1880? What else could be blocking this port access if not firewall or anti virus software?
I have found the issue: Hyper-V was reserving some ports on windows.
netsh int ip show excludedportrange protocol=tcp

HTTPs port error on NodeJs and Express, why? banging my head on wall

Why do I get an error on my Amazon server when running my NodeJS app?
sudo node app.js
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/ubuntu/www/app.js:65:38)
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)
Here is my code:
var app = express();
var server = http.createServer(app).listen(4000, function() {
console.log('Express HTTP server listening on port ' + app.get('port'));
});
https.createServer(credentials, app).listen(443, function() {
console.log('Express HTTPS server listening on port 443');
});
I think the problem is the 443 because when I delete the https.createServer "listen()" connection then everything works fine.
Thanks for your attention :)
You probably have another process running that's using this port (probably another instance of your app.
Use ps aux to see your running processes.
Use lsof -i | grep 4000 to see what's using this port.

Port in use when launching Node.js app with Heroku Foreman

I am trying to deploy my node.js app to heroku but when I try to launch it locally using foreman I am getting Error: listen EADDRINUSE. I have run a netstat and grepped for the port nothing else is using it and the server starts without issue when running directly as a node http server.
The app I am trying to deploy is using mongo and redis I am not sure if these components will effect the server starting with Foreman. Does anyone have any suggestions on areas I could look at for potential bugs?
foreman start
01:37:18 web.1 | started with pid 1835
01:37:18 web.1 | /usr/local/foreman/lib/foreman/process.rb:66: warning: Insecure world writable dir /usr/local in PATH, mode 040777
01:37:19 web.1 | events.js:72
01:37:19 web.1 | throw er; // Unhandled 'error' event
01:37:19 web.1 | ^
01:37:19 web.1 | Error: listen EADDRINUSE
01:37:19 web.1 | at errnoException (net.js:863:11)
01:37:19 web.1 | at Server._listen2 (net.js:1008:14)
01:37:19 web.1 | at listen (net.js:1030:10)
01:37:19 web.1 | at Server.listen (net.js:1096:5)
01:37:19 web.1 | at Function.app.listen (/Users/craig/Documents/Sandboxes /xxx/node_modules/express/lib/application.js:535:24)
01:37:19 web.1 | at Object.<anonymous> (/Users/craig/Documents/Sandboxes/xxx/web.js:25:5)
01:37:19 web.1 | at Module._compile (module.js:456:26)
01:37:19 web.1 | at Object.Module._extensions..js (module.js:474:10)
01:37:19 web.1 | at Module.load (module.js:356:32)
01:37:19 web.1 | at Function.Module._load (module.js:312:12)
01:37:19 web.1 | exited with code 8
01:37:19 system | sending SIGTERM to all processes
SIGTERM received
Thanks.
--Additional information--
The procfile just has one entry:
web: node web.js
and I have set the listener up as follows:
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});
I just ran into this on OS X. It looks like foreman picks port 5000 by default, which seems to conflict with Bonjour/mDNS/UPNP services. (From what I've read. I haven't taken the time to pick out which it is.)
However, you can change the port that foreman uses in two ways: specify the port on the command line or create a .foreman file with the port number specified there.
Good luck and happy coding!
I had the same issue, Error: listen EADDRINUSE means that a Node server is already running.
Check that you are not running a Node server for the same project locally. If you are working on a GitHub synced project locally (on port 5000 for instance) which is tied to Heroku you cannot run a local Node server for that project as the port will be in use twice.
I was actually running a Node server on a project in a different Terminal window and didn't notice immediately.

Resources