'Is there a way to always kill ports when it gets terminated on VC Code on mac?

I'm using a MacBook Pro and recently I have come across this issue while running VSCode. Although, I kill the port on the VSCode integrated terminal, but it's still running somewhere in the background. Is there a way to kill ports when you kill it on VSCode, so I don't have to do the npx kill-port 5000 for instance?

I've run this command npx kill-port 5000 on Terminal, but it seems like it doesn't kill the port.

I get this error, when I run another project on VSCode:

Emitted 'error' event on WebSocketServer instance at:
    at Server.emit (node:events:538:35)
    at emitErrorNT (node:net:1357:8)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'EADDRINUSE',
  errno: -48,
  syscall: 'listen',
  address: '::',
  port: 5000
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source