Kill process running on a port in node.js

417 views Asked by At

I am trying to start a process on port 3000 and 3080 in a node.js app. I want to check if these ports are already running processes and want to kill those processes.

I tried portscanner but no luck.

portscanner.findAPortInUse([3000], '0.0.0.0').then(port => {
  console.log(`Port ${port} is available!`);
  (async () => {
    await killPortProcess(3000);
  })();
});

How can I achieve this?

1

There are 1 answers

0
Sam On

Try this:

exec("kill $(lsof -t -i:3000)", async function(err, stdout, stderr) {
  console.log(stdout)
});