0

I'm running a NodeJS application on ubuntu LTS 20.4 managed by PM2. Application is running fine but when I check the logs I see lots of EADDRINUSE address already in use message.

I started the server using the command sudo pm2 start index.js

Error: listen EADDRINUSE: address already in use :::8000
    at Server.setupListenHandle [as _listen2] (node:net:1432:16)
    at listenInCluster (node:net:1480:12)
    at Server.listen (node:net:1568:7)
    at file:///home/ubuntu/wapi/index.js:105:10
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'EADDRINUSE',
  errno: -98,
  syscall: 'listen',
  address: '::',
  port: 8000
}
cleanup

Stack trace is pointing to line number 105 of the file below.

https://github.com/billbarsch/myzap/blob/myzap2.0/index.js

What I don't understand is why PM2 is trying to start the server almost every second (because this message appears in the log every second) when the service is already running?

And sudo pm2 ls is listing 2 processes

│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │

│ 0 │ index │ default │ 1.0.0 │ fork │ 1673211 │ 103s │ 130 │ online │ 0% │ 111.8mb │ root │ disabled │

│ 1 │ index │ default │ 1.0.0 │ fork │ 1673848 │ 2s │ 450… │ online │ 66.7% │ 120.3mb │ root │ disabled │

Really appreciate some help.

Thanks

Jugs
  • 43
  • 8

2 Answers2

2

It appears that you already have another process of pm2 which is running the same application. That is why you are seeing EADDRINUSE.

And the reason you are getting the same log every second is that pm2 tends to restart the application when it errors out.

You can stop all the processes using

pm2 stop all

And then try to re-run your process.

Amit Phulera
  • 158
  • 10
  • that can be a every process on server, not only a process run under PM2 – Alaindeseine Aug 19 '22 at 10:08
  • I think you didn't properly understand my question. "Application is running fine but when I check the logs I see lots of EADDRINUSE address already in use message." The port is being used by my application. My question is why is PM2 trying to run my application again when it is already running and which is why I get the message "address already in use" in the logs – Jugs Aug 20 '22 at 06:10
0

Your error tell that another process already use the specified port. That can be every process on your server, not only a node process running under PM2.

To determine what process already use the port, you can issue the netstat command:

netstat -ano -p -t | grep 8000

This will print out ALL process connected to this port, server as client. To identify server process look for LISTEN.

If not logged as privileged user, use sudo:

sudo netstat -ano -p -t | grep 8000
Alaindeseine
  • 3,260
  • 1
  • 11
  • 21
  • I think you didn't properly understand my question. "Application is running fine but when I check the logs I see lots of EADDRINUSE address already in use message." The port is being used by my application. My question is why is PM2 trying to run my application again when it is already running and which is why I get the message "address already in use" in the logs – Jugs Aug 20 '22 at 06:10
  • I don't think i don't understand question. Your datas is clear. You show two process when pm2 list command is executed. First is running for 103s and second is running for 2s, so this is the second one which is generating the error. And the error is because the second process try to use the same port. Where come from the second process? you should provide informations about that. – Alaindeseine Aug 20 '22 at 08:51