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

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>

Related

Run NodeJS server from AWS EC2

EC2 is properly deployed with my NodeJS server but I can't figure out how to keep it running without an ssh connection to the linux machine and running npm start
For Example:
[ec2-user#ip-172-31-89-105 me_server]$ npm start
> me-server#1.0.0 start
> nodemon index.js
[nodemon] 2.0.20
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
Server Started at 8080
Database Connected
I can run this command from an ssh tunnel from my local machine but I don't want my local machine to be in control of the server connection, how can I tell EC2 to run npm start on its own and keep it running indefinitely?

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

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

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.

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

App crashed in nodemon

I am trying to run in debug mode using Git Bash a nodejs application.
I did it several times but on the recent ones it doesn't work anymore.
When I run the command:
npm run startwindows:inspect
it says:
14 Apr 15:10:59 - [nodemon] v1.4.1
14 Apr 15:10:59 - [nodemon] to restart at any time, enter `rs`
14 Apr 15:10:59 - [nodemon] ignoring: C:\XXX\.git/**/* C:\XXX\node_modules/**/* C:\XXX\bower_components/**/* .sass-cache
14 Apr 15:10:59 - [nodemon] watching: C:\XXX\server/**/* C:\XXX\config-local/**/*
14 Apr 15:10:59 - [nodemon] watching extensions: js,json,yaml
14 Apr 15:10:59 - [nodemon] starting `node --inspect --debug-brk energyreports.js`
14 Apr 15:10:59 - [nodemon] child pid: 6352
Unable to open devtools socket: address already in use
14 Apr 15:10:59 - [nodemon] app crashed - waiting for file changes before starting...
14 Apr 15:11:03 - [nodemon] watching 37,149 files
14 Apr 15:11:03 - [nodemon] watching 37,149 files - this might cause high cpu usage. To reduce use "--watch".
What can I provide more is that startwindows:inspect is a script from the package.json file and it looks like this:
"startwindows:inspect":"set NODE_ENV=dus&& set NODE_CONFIG_DIR=./config-local/&& nodemon -V -w server -w config-local -e js,json,yaml --inspect --debug-brk| bunyan -o short"
What means by using --watch? Is this a normal case when testing it in debug? My CPU % utilization looks fine all the time.
Well it says exactly what the problem is.
Unable to open devtools socket: address already in use
Previous instance of the app is still running. Just restart your computer or kill related running processes.
New Edit: By OwlyMoly
Killing processes at a particular port: (In Mac)
If you know the port where your app running at, then check for that port ID and kill the process.
To check port id:
lsof -i :YourPort
or
lsof -n -iTCP:YourPort | grep LISTEN
Then get PID from the result and use the below command to kill that process.
To Kill process using PID:
kill -9 PID

Resources