How can I start a NodeJS in the background? - node.js

I tried to start a project
node index.js , I got this
events.js:173
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (net.js:1255:14)
at listenInCluster (net.js:1303:12)
at Server.listen (net.js:1391:7)
at Function.listen (/home/forge/bheng/public/ios-base-api/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/home/forge/bheng/public/ios-base-api/index.js:156:5)
at Module._compile (internal/modules/cjs/loader.js:721:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
Emitted 'error' event at:
at emitErrorNT (net.js:1282:8)
at internalTickCallback (internal/process/next_tick.js:72:19)
at process._tickCallback (internal/process/next_tick.js:47:5)
at Function.Module.runMain (internal/modules/cjs/loader.js:777:11)
at executeUserCode (internal/bootstrap/node.js:342:17)
at startExecution (internal/bootstrap/node.js:276:5)
at startup (internal/bootstrap/node.js:227:5)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
Then, I ran lsof -i | grep 3000, I got
node 6159 root 19u IPv6 58371978 0t0 TCP *:3000 (LISTEN)
Then, I ran ps -aux | grep node, I got
root 448 0.0 1.4 569912 29600 ? Ssl 21:21 0:00 node /usr/bin/pm2 update
root 1266 0.0 1.5 569400 31628 ? Ssl 21:28 0:00 node /usr/bin/pm2 update
root 6159 1.0 2.4 845540 49568 ? Ssl 21:46 0:02 node /home/forge/bheng/public/ios-base-api/index.js
root 6482 0.0 0.0 11244 948 pts/3 S+ 21:50 0:00 grep --color=auto node
root 32233 0.0 1.4 569912 29876 ? Ssl 21:14 0:00 node /usr/bin/pm2 update
I just want to start my nodejs app in the background and be able to access my endpoint anytime.
Why is this so complicated ? 😭

There are many packages which can help you. Check out pm2, forever
Both of them allow you to run your project in background and auto-reload is possible too :)

Related

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.

How to fix Listen EADDRINUSE in ubuntu

How can I resolve this issue?
1|server | at listenInCluster (net.js:1327:12)
1|server | at Server.listen (net.js:1414:7)
1|server | at Object.<anonymous> (/root/signalmaster/server.js:41:8)
1|server | at Module._compile (internal/modules/cjs/loader.js:776:30)
1|server | at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
1|server | at Module.load (internal/modules/cjs/loader.js:653:32)
1|server | at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
1|server | at Function.Module._load (internal/modules/cjs/loader.js:585:3)
1|server | at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:27:21)
1|server | code: 'EADDRINUSE',
1|server | errno: 'EADDRINUSE',
1|server | syscall: 'listen',
1|server | address: '::',
1|server | port: 8080 }
My server and turnserver status is Online but our clients can't communicate or see each other.
UPDATE:
see this pm2 list
try in the console :
killall node
and the problem should be fixed
The following IP address already used. In same IP address you can run the server by different ports. The port must be opened.
* If it's live server and already running httpd .
<VirtualHost *:80>
ServerName api.xxxx.com
#DocumentRoot /var/www/test
CustomLog /var/www/test/access.log common
ErrorLog /var/www/test/error.log
UseCanonicalName Off
UserDir disabled
<Location "/">
ProxyPreserveHost On
ProxyPass http://localhost:8080/
ProxyPassReverse http://localhost:8080/
</Location>
</VirtualHost>

Getting Error of Unhandled 'error' event events.js Angular 6

I am new to angular and trying to setup an existing project on the server. I have setup the project on the root of the server and moved my build to apache folder (/var/www/html). I have a carousel on the home page and getting errors of main.da2430b946ec091e1c9e.js:1 ERROR TypeError: Cannot read property 'toArray' of undefined. I am using bootstrap carousel on home page.
When i run the command node project.js which have the NPM PACKAGES API ROUTES ADMIN ROUTES defined, i am getting error:
events.js:183
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::8081
at Server.setupListenHandle [as _listen2] (net.js:1360:14)
at listenInCluster (net.js:1401:12)
at Server.listen (net.js:1485:7)
at Object.<anonymous> (/home/sam/server/routes/chat.js:36:8)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/home/sam/server/routes/api.js:26:18)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
I read somewhere that this error might be because of the port being used by other service. So i run below comands:
sudo lsof -i :80 # checks port 81
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
apache2 886 sam 4u IPv6 18050 0t0 TCP *:http (LISTEN)
apache2 2559 www-data 4u IPv6 18050 0t0 TCP *:http (LISTEN)
apache2 2588 www-data 4u IPv6 18050 0t0 TCP *:http (LISTEN)
sudo lsof -i :80 # checks port 80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
apache2 886 sam 4u IPv6 18050 0t0 TCP *:http (LISTEN)
apache2 2559 www-data 4u IPv6 18050 0t0 TCP *:http (LISTEN)
apache2 2588 www-data 4u IPv6 18050 0t0 TCP *:http (LISTEN)
sudo lsof -i :8081 # checks port 80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 8468 sam 10u IPv6 54277 0t0 TCP *:tproxy (LISTEN)
sudo lsof -i :8081 # checks port 8081
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 8468 sam 10u IPv6 54277 0t0 TCP *:tproxy (LISTEN)
Here is the Angular version details:
Angular CLI: 6.0.8
Node: 8.14.0
OS: linux x64
Angular: 6.0.9
Can anyone help me with this issue. I have spend many hours on it and didn't found any working solution.
Thanks in Advance.

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

Error: listen EADDRINUSE :::443 error when trying to start nodejs server

I am trying to create a nodejs application that uses SSL. I have the cert, domain, and nodejs configured and working, but I'm having a problem sometimes when I stop my node server using Ctrl+C. I am getting the following error when trying to restart the node server:
Error: listen EADDRINUSE :::443
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at Server._listen2 (net.js:1237:14)
at listen (net.js:1273:10)
at Server.listen (net.js:1369:5)
at Object.<anonymous> (/var/www/html/server.js:212:47)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
Now I know this means that the port is in use, but I've tried several things to find the process that is using that port but I cant. A couple thing I've tried that are worth mentioning are netstat:
$ sudo netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1164/sshd
tcp6 0 0 :::22 :::* LISTEN 1164/sshd
udp 0 0 0.0.0.0:68 0.0.0.0:* 882/dhclient
and ps:
$ ps aux |grep node
ubuntu 7632 0.0 0.0 12944 988 pts/0 S+ 23:30 0:00 grep --color=auto node
but neither of them show me what is using port 443. I have pm2 to keep my node server in a running state, but it keeps saying errored and then I find the EADDRINUSE errors in the pm2 error log. I've been banging my head against the wall for hours. Can anyone assist me? Thanks!
I had this issue and the only command that revealed the process was
sudo lsof -iTCP -sTCP:LISTEN -P

Resources