I have the following node.js server:
const main = () => {
log(`Starting server`)
const exit = signal => {
server.close(() => process.exit())
log('Server is closed.')
}
const server = http.createServer(handleRequest)
server.listen(process.env.PORT, process.env.HOST, () => log(`Server is listening on ${process.env.HOST}:${process.env.PORT}`))
process.on('SIGING', exit)
process.on('SIGTERM', exit)
process.on('uncaughtException', (err, origin) => {
log(`Process caught unhandled exception: ${err} ${origin}`, 'ERROR')
})
}
When I first run the server, I see:
Server is listening on 127.0.0.1:8080
(which is expected)
I kill the server with ctrl-c
Subsequent attempts to start the server output:
[ 2023-05-09T00:29:53.264Z | INFO ] Process caught unhandled exception: Error: listen EADDRINUSE: address already in use "8080" uncaughtException ERROR
I have tried the following:
lsof
netstat
fuser
npx kill-port 8080
brew install htop
- restarting the computer
specifics:
$ sudo lsof -i :8080
$ sudo lsof -t -i :8080
$ sudo lsof -t -i tcp:8080
$ sudo lsof -i tcp:8080
$ sudo lsof -i :8080 -v
$ sudo lsof > ~/temp_lsof.txt
$ sudo lsof -iTCP:8080 -sTCP:LISTEN
$ sudo netstat -tuln | grep 8080
$ sudo netstat -lnp | grep 8080
$ sudo netstat -tanl | grep 8080
$ sudo netstat -anp tcp | grep 8080
Each and every one of these commands resulted in no output whatsoever.
╭ ~/Desktop/my_api (express-postrgesql)
╰ % npx kill-port 8080
Could not kill process on port 8080. No process running on port.
╭ ~/Desktop/my_api (express-postrgesql)
╰ % node server.js
[ 2023-05-09T01:13:24.247Z | INFO ] Starting api server
[ 2023-05-09T01:13:24.249Z | INFO ] Process caught unhandled exception: Error: listen EADDRINUSE: address already in use "8080" uncaughtException ERROR
I can't seem to find a process listening on port 8080 and yet... node insists something is. Any suggestions?
EDIT:
Switched the PORT
in .env
to 8081
to test (something looks screwy with the fact that the port is in ""
...):
╭ ~/Desktop/my_api (express-postrgesql)
╰ % node server.js
[ 2023-05-09T01:17:50.148Z | INFO ] Starting api server
[ 2023-05-09T01:17:50.150Z | INFO ] Server is listening on "127.0.0.1":"8081"
^C
╭ ~/Desktop/my_api (express-postrgesql)
╰ % node server.js
[ 2023-05-09T01:17:59.339Z | INFO ] Starting api server
[ 2023-05-09T01:17:59.341Z | INFO ] Process caught unhandled exception: Error: listen EADDRINUSE: address already in use "8081" uncaughtException ERROR
╭ ~/Desktop/my_api (express-postrgesql)
╰ % sudo lsof -i :8081
Password:
╭ ~/Desktop/my_api (express-postrgesql)
╰ %