Getting an error while running "nodemon app" comannd - node.js

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.

Related

how to correctly configure ngrok docker container for node or react app?

I'm trying to use ngrok for exposing my localhost to share my node backend with a friend.
The problem I face is that whenever I start node backend the ngrok container couldn't work cause it trys to map the same port as being used by node.
node:events:371
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::5000
at Server.setupListenHandle [as _listen2] (node:net:1319:16)
at listenInCluster (node:net:1367:12)
at Server.listen (node:net:1454:7)
at Function.listen (C:\Users\928941\node_backends\express-for-react\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (C:\Users\928941\node_backends\express-for-react\app.js:27:5)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153: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:79:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1346:8)
code: 'EADDRINUSE',
errno: -4091,
syscall: 'listen',
address: '::',
port: 5000
}
[nodemon] app crashed - waiting for file changes before starting...
the above error simply means that port 5000 is currently in use(i.e., by ngrok docker container).
If I run the node app first and try to run the docker container after that I get this error from docker.
[+] Running 1/0
- Container docker-compose_for_ngrok_ngrok_1 Created 0.0s
Attaching to ngrok_1
Error response from daemon: Ports are not available: listen tcp 0.0.0.0:5000: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.
This is my docker-compose.yml file
version: '3'
services:
ngrok:
image: ngrok/ngrok:alpine
ports:
- 4040:4040
- 5000:5000
environment:
- NGROK_AUTHTOKEN=my_authentication_token
command: http 5000
the compose file is same as this docker run command so anyone can be used.
docker run -d -p 4040:4040 -p 5000:5000 -e NGROK_AUTHTOKEN=my_token ngrok/ngrok:alpine http 5000
Port 4040 is default ngrok port for dashboard and it works fine for me.
I'm also facing this issue with react app as well.
I'm not sure if you are running on linux, but you might not need to do any port forwarding if you use the --net=host option. See the examples in https://hub.docker.com/r/ngrok/ngrok/
The equivalent for a docker compose file is network_mode:https://docs.docker.com/compose/compose-file/compose-file-v3/#network_mode
So your docker compose would be:
version: '3'
services:
ngrok:
image: ngrok/ngrok:alpine
environment:
- NGROK_AUTHTOKEN=my_authentication_token
command: http 5000
network_mode: "host"
Assuming your node is running on localhost:5000, if you start a docker container and use the same network, then you don't need to open any ports.
If you are using mac or windows, you will need to use the workaround here:
https://docs.docker.com/desktop/mac/networking/#use-cases-and-workarounds
Which is to use command: http host.docker.internal:5000 instead.

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.

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>

Port specified when running node is undefined

The port is specified when i ran the node project says undefined.
> sf-chain#1.0.0 dev E:\System\dev\node\sf-chain
> nodemon ./app "HTTP_PORT=3002" "P2P_PORT=5002" "PEERS=ws://localhost:5001"
[nodemon] 1.18.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node ./app HTTP_PORT=3002 P2P_PORT=5002 PEERS=ws://localhost:5001`
process.env.HTTP_PORT :: undefined
HTTP_PORT :: 3001
Listening for peer-to-peer connections on: 5001
events.js:160
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::3001
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at Server._listen2 (net.js:1259:14)
at listen (net.js:1295:10)
at Server.listen (net.js:1391:5)
at EventEmitter.listen (E:\System\dev\node\sf-chain\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (E:\System\dev\node\sf-chain\app\index.js:28:5)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
[nodemon] app crashed - waiting for file changes before starting...
I ran the first instance using
npm run dev
const HTTP_PORT = process.env.HTTP_PORT || 3001;
The application starts successfully running on port 3001 since i didn't specify HTTP_PORT at the run command.
But when trying to run another instance by specifying HTTP_PORT using the below command
npm run dev HTTP_PORT=3002 P2P_PORT=5002 PEERS=ws://localhost:5001
I get this error.
Error: listen EADDRINUSE :::3001
Which means the HTTP_PORT specified at run time is seen as undefined that's why it's trying to using the 3001 port for the first instance.
Error: listen EADDRINUSE
- error comes when somthing is already running/listen on that port
Its because sometime instance of same nodejs acquire port and didnt kill/exit the process properly,
if you have Ubuntu check first which process is using port by
lsof -i :3001 // Port no.
then output will be some thing like this.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 2496 nasiruddin 13u IPv6 37696 0t0 TCP *:3001 (LISTEN)
kill using PID
kill 2496
then start again

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