nodemon giving this error code: 'EADDRINUSE', errno: -4091, syscall: 'listen', address: '::', port: 5000 - node.js

Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1347:8)
at processTicksAndRejections (internal/process/task_queues.js:82:21) {
code: 'EADDRINUSE',
errno: -4091,
syscall: 'listen',
address: '::',
port: 5000
}
[nodemon] app crashed - waiting for file changes before starting...

Having the same problem but I have 3 fixes for this you can use any
1. a permanent and long way:
i. Install the kill-port node package as a dev dependency:
npm install kill-port --save-dev
ii. Create a nodemon.json file in the root of your project containing:
{
"events": {
"restart": "kill-port 5000",
"crash": "kill-port 5000"
},
"delay": "1500"
}
iii. Then, in your package.json file, have something like this:
"scripts": {
"start-dev": "nodemon app.js",
}
iv. Then start your app in dev mode with:
npm run start-dev
2. Manually kill from the system(easy fix):
directly kill the port with the following command.
fuser -n tcp -k 5000
3. Restarting the system:
restarting the project
restarting the computer

For windows
netstat -ano | findstr : 5000
(output : TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 18264)
taskkill /PID 18264 /f
Mainly this error comes when we run our code on same port or port already busy, so we have to kill the process
For ubuntu
fuser -k 5000/tcp

Related

cant access the webapp running in docker with docusaurus

I have built a docker container with docusaurus server and opened port, but the website is not accessible and throws an error like this. How do I allow all the ip address in the docusaurus server?
yarn run v1.22.19
warning package.json: No license field
$ docusaurus-start
LiveReload server started on port 35729
Docusaurus server started on port 3000
Request failed: Error: connect ECONNREFUSED 127.0.0.1:80
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1247:16) { errno: -111, code: 'ECONNREFUSED', syscall: 'connect',
address: '127.0.0.1', port: 80 }

PORT 3306 Already In use when i rn nodemon in NodeJS app

I have a nodejs app which was running just fine all this time until I opened MySQL Workbench and also included .env file in my project. I was in the process of deploying the system to Digital Ocean managed database service when I opened MySQL Workbench to visualise the process and not use the mysql shell. Everything worked fine and I migrated my db on to the DO database cluster.
I also wanted to make my app more secure, so I bumped into the .env file method and tried my best to follow through and I came up with this:
Step 1:
npm i dotenv --save
Step 2: Added require('dotenv').config() to my server.js file
Step 3: Update my DB connection file
const mysql = require("mysql");
const conn = mysql.createConnection({
host: process.env.HOST,
user: process.env.USERNAME,
password: process.env.PASSWORD,
//rsport : (process.env.PORT),
database: process.env.DATABASE_NAME,
multipleStatements: true,
});
conn.connect((err) => {
if (err) {
console.log("Oops!, Failed to connect to the database.");
} else {
console.log("Database connection succesfull!");
}
});
module.exports = conn;
Step 4: I set my local .env file and remote .env file accordingly
Step 5: I run nodemon the it return the following error:
body-parser deprecated undefined extended: provide extended option server.js:27:17
events.js:292
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3306
at Server.setupListenHandle [as _listen2] (net.js:1318:16)
at listenInCluster (net.js:1366:12)
at Server.listen (net.js:1452:7)
at Function.listen (G:\Maxiko Payment System\Systems\Management Apps\Microservices\mx-core\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (G:\Maxiko Payment System\Systems\Management Apps\Microservices\mx-core\server.js:47:5)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1345:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EADDRINUSE',
errno: -4091,
syscall: 'listen',
address: '::',
port: 3306
}
[nodemon] app crashed - waiting for file changes before starting...
What am I doing wrong? honestly I do not think .env has anything to do with this, but i definately know MySQL Workbench made its own connection. So am I to believe that only one application can connect to a database at one time? That doesnt sound right to me either.
Try running your node js server on a different port or run this command in command prompt (as administrator)
netstat -ano | findstr :<PORT>
Replace with your port number that is in use(in your case 3306)
Then it will show you the PID of your process
Something like this
TCP 0.0.0.0:3306 0.0.0.0:0 LISTEN 77777
77777 is the PID
then run this command
taskkill /PID <PID> /F
Replace with the PID you got in the last command
Its look like some other process already running on your port 3306
You can check it by using below command
lsof -i tcp:3306
It will list you process that is already on the port 3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 12012 user 20u IPv6 86535 0t0 TCP *:3306 (LISTEN)
That shows a process with PID 12012 already using your port 3306
If this process is redundant you can kill it by following command:
sudo kill -9 PID
Replace PID with your process id which in my case is 12012
otherwise you can use some other port to run your node server
I have found the problem. There was indeed another process running and using the same port. In app server initialization script , I was setting the port like this:
app.use('port', process.env.PORT || 5000)
Where at the the fdatabase connection file, I set the ports also as
const conn = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
port : (process.env.DB_PORT),
database: process.env.DB_NAME,
multipleStatements: true,
});
So, as you can see, there is supposed to be a port conflict since i only had on PORT=3006 variable in the .env file.
SOLUTION
All I had to do was specify the database port as
DB_PORT = 3006
APP_PORT = 5000
in the environment variables.
Thank you and Goog luck.

Getting an error while running "nodemon app" comannd

i've downloaded express package and nodemon, I created a server and rendered all of my ejs files . while running "nodemon app" command I'm getting this error, what specifically the problem is? :
PS C:\Users\user\Desktop\my port> nodemon app
[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
events.js:291
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (net.js:1316:16)
at listenInCluster (net.js:1364:12)
at Server.listen (net.js:1450:7)
at Function.listen (C:\Users\user\Desktop\my port\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (C:\Users\user\Desktop\my port\app.js:16:5)
at Module._compile (internal/modules/cjs/loader.js:1251:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1272:10)
at Module.load (internal/modules/cjs/loader.js:1100:32)
at Function.Module._load (internal/modules/cjs/loader.js:962:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1343:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EADDRINUSE',
errno: -4091,
syscall: 'listen',
address: '::',
port: 3000
}
[nodemon] app crashed - waiting for file changes before starting...
Your system has something already running on port 3000.
you can do either of 2,
Kill process running on 3000
Or Use different port in your current app (like 3001, 5000, etc)
hi use this and kill the process directly;
netstat -ano | findstr :XXXX
XXXX <<<<----put the number of the port you have problem, system show it always, then when you press enter the system show you a message with a real port that one is what you need killing, so put this:
taskkill /F /PID XXXX
XXXX <<<<----the number of the port you want to kill and what system gave you after the first command
type this in command prompt
netstat -ano | findstr :XXXX <-THIS IS THE PORT NUMBER YOU used
And it will give you something like this
TCP 0.0.0.0:8080 || 0.0.0.0:0 || LISTENING | tttt
TCP [::]:8080 || [::]:0 || LISTENING || tttt<--thiss is the number you needed
then type this in command prompt
taskkill /PID tttt /F <--then enter
result will be something like -->
The process with PID tttt has been terminated.

Dev Server restart repeatedly results in: code: 'EADDRINUSE'

I find myself having to cycle through the following pattern in terminal while using nodemon:
Emitted 'error' event at:
at emitErrorNT (net.js:1253:8)
at processTicksAndRejections (internal/process/task_queues.js:84:9) {
code: 'EADDRINUSE',
errno: 'EADDRINUSE',
syscall: 'listen',
address: '::',
port: 8081
}
[nodemon] app crashed - waiting for file changes before starting...
^C
owner#G700:~/PhpstormProjects/shopify/from_tch$ sudo lsof -i :8081
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 28169 owner 19u IPv6 26913489 0t0 TCP *:tproxy (LISTEN)
owner#G700:~/PhpstormProjects/shopify/from_scratch$ kill -9 28169
owner#G700:~/PhpstormProjects/shopify/from_scratch$ nodemon server.js
[nodemon] 1.19.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
> Ready on http://localhost:8081
nodemon is supposed to handle restarting the server on changes, so I'm not sure why it seems to keep conflicting with itself and forcing me to kill processes. Does anyone know if this is a misconfiguration, or how to overcome this? I'm on Lubuntu 18.04 and my terminal is this:
$ bash --version
GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

nodejs error EADDRINUSE

I get this when i type "node site.js" im 100% sure im not running this node twice.
Error: listen EADDRINUSE :::3000
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at Server._listen2 (net.js:1262:14)
at listen (net.js:1298:10)
at Server.listen (net.js:1376:9)
at Server.listen.Server.attach (/root/node_modules/socket.io/lib/index.js:228:9)
at Timeout._onTimeout (/root/nodejs/site.js:1005:29)
at ontimeout (timers.js:365:14)
at tryOnTimeout (timers.js:237:5)
at Timer.listOnTimeout (timers.js:207:5)
code: 'EADDRINUSE',
errno: 'EADDRINUSE',
syscall: 'listen',
address: '::',
port: 3000 }
Im using FEDORA 23
Your site.js tries to listen twice on that port or something (another process) is already listening on port 3000. Find the service and stop/kill it. This command should help: lsof -i | grep 3000
Try running killall node, then node site.js again. If that doesn't work, at least you can rule out Node from the cause of this.
Your site is listening twice to that port. You can check in your code whether you have 2 instructions .listen()

Resources