What is kill-port?
The kill-port npm package is a utility that allows you to kill processes running on specific ports. This can be particularly useful for developers who need to free up ports that are being used by other applications or processes.
What are kill-port's main functionalities?
Kill a specific port
This feature allows you to kill a process running on a specific port. In this example, the process running on port 3000 is terminated.
const kill = require('kill-port');
kill(3000)
.then(() => {
console.log('Port 3000 killed');
})
.catch((err) => {
console.error('Error killing port 3000:', err);
});
Kill a port with a specific protocol
This feature allows you to specify the protocol (TCP or UDP) when killing a port. In this example, the TCP process running on port 3000 is terminated.
const kill = require('kill-port');
kill(3000, 'tcp')
.then(() => {
console.log('TCP process on port 3000 killed');
})
.catch((err) => {
console.error('Error killing TCP process on port 3000:', err);
});
Kill multiple ports
This feature allows you to kill multiple ports simultaneously. In this example, the processes running on ports 3000 and 4000 are terminated.
const kill = require('kill-port');
Promise.all([kill(3000), kill(4000)])
.then(() => {
console.log('Ports 3000 and 4000 killed');
})
.catch((err) => {
console.error('Error killing ports:', err);
});
Other packages similar to kill-port
fkill
fkill is a package that allows you to kill processes by port, name, or PID. It provides a more versatile approach compared to kill-port, which is focused solely on killing ports.
kill-process-by-port
kill-process-by-port is another package that specifically targets processes running on a given port. It is similar to kill-port but may have different implementation details and options.
port-killer
port-killer is a utility for killing processes running on specific ports. It offers similar functionality to kill-port but may have different command-line options and usage patterns.
kill-port
Kill process running on given por
Table of Contents
Install
With npm
:
npm install --save kill-port
With yarn
:
yarn add kill-port
With pnpm
:
pnpm add kill-port
Usage
const kill = require('kill-port')
const http = require('http')
const port = 8080
const server = http.createServer((req, res) => {
res.writeHead(200, {
'Content-Type': 'text/plain'
})
res.end('Hi!')
})
server.listen(port, () => {
setTimeout(() => {
kill(port, 'tcp')
.then(console.log)
.catch(console.log)
}, 1000)
})
API
The module exports a single function that takes a port number as argument. It returns a promise.
CLI
$ npm install --global kill-port
$ yarn global add kill-port
Then:
$ kill-port --port 8080
$ kill-port 9000
$ kill-port 9000 --method udp
You can also kill multiple ports:
$ kill-port --port 8080,5000,3000
$ kill-port 9000 3000 5000
Contributing
Got an idea for a new feature? Found a bug? Contributions are welcome! Please open up an issue or make a pull request.
License
MIT © Tiaan du Plessis