A simple npm package to help you identify and terminate processes using specific ports on your system.
npm install port-terminatorPort Terminator is an npm library for managing and terminating processes associated with specific ports on your system.
``shell`
npm install port-terminatorOR
yarn add port-terminator
Import the library in your Node.js application:
`javascript`
const { portTerminator, isPortOpen, portsTerminator } = require("port-terminator");
// or using ES6 import
import { portTerminator, isPortOpen, portsTerminator } from "port-terminator";
Use portTerminator to terminate processes associated with a single port:
`javascriptProcesses associated with port ${portToTerminate} terminated successfully.
const portToTerminate = 8080;
try {
await portTerminator(portToTerminate);
console.log();Error terminating processes for port ${portToTerminate}:
} catch (error) {
console.error(, error);`
}
Use isPortOpen to check if a port is open:
`javascriptPort ${portToCheck} is open.
const portToCheck = 3000;
try {
const isOpen = await isPortOpen(portToCheck);
if (isOpen) {
console.log();Port ${portToCheck} is closed or invalid.
} else {
console.log();Error checking port status for port ${portToCheck}:
}
} catch (error) {
console.error(, error);`
}
Use portsTerminator to terminate processes associated with multiple ports:
`javascriptProcesses associated with port ${port} terminated successfully.
const portsToTerminate = [8080, 3000, 5000];
try {
const terminationResults = await portsTerminator(...portsToTerminate);
terminationResults.forEach((response, port) => {
if (response.status) {
console.log();Error terminating processes for port ${port}:
} else {
console.error(, response.error);`
}
});
} catch (error) {
console.error("Error terminating processes:", error);
}
You can use port-terminator as a global package.
Install the package globally using npm:
`shell`
npm install --global port-terminatorOR
yarn global add port-terminator
#### Terminating Processes for a Single Port
To terminate processes associated with a single port, run the following command:
`shell`
port-terminator 8080
Replace 8080 with the port number you want to terminate processes for.
#### Terminating Processes for Multiple Ports
You can also terminate processes for multiple ports by specifying multiple port numbers:
`shell`
port-terminator 9000 3000 5000
Replace 9000, 3000, and 5000` with the port numbers you want to terminate processes for.
> Please note that you need administrative or superuser privileges to terminate processes on some systems. Ensure you have the necessary permissions before using this command.
This library is open-source and available under the MIT License.