clean-port
v0.1.2
Published
Library to clear a TCP port by terminating the process that uses it.
Maintainers
Readme
clean-port
Clear a TCP port by terminating the process that currently owns it. Works on macOS, Linux, and Windows.
Requirements
- macOS or Linux:
lsofmust be available - Windows:
netstatmust be available (included with Windows)
Install
npm install clean-port -gQuick start
Library
const { clearPort } = require("clean-port");
const result = clearPort(3000);
console.log(result);Force cleanup if the port remains in use:
const { clearPort } = require("clean-port");
const result = clearPort(3000, { force: true });
console.log(result);CLI
npx clean-port 3000
npx clean-port 3000 --force
npx clean-port 3000 --signal SIGKILLCLI options
-s,--signalkill signal (defaultSIGTERM)-f,--forceuseSIGKILLif still in use--wait-msdelay between checks (default300)--retriesnumber of rechecks (default5)
API
clearPort(port, options)
portnumber or numeric stringoptions.signalkill signal (defaultSIGTERM)options.waitMsdelay between checks in ms (default300, non-negative)options.retriesnumber of rechecks (default5, non-negative integer)options.forceuseSIGKILLif still in use (defaultfalse)
Returns:
{ cleared: boolean, status: "cleared" | "force_cleared" | "already_clear" | "still_in_use", pids: number[], remainingPids: number[] }
Best practices
- Start with
SIGTERMand allow a short wait before retrying. - Use
forceonly when you can safely terminate the process. - Run as the same user that owns the process; use
sudoonly when required. - Avoid auto-restart supervisors while cleaning a port in dev scripts.
- For CI/automation, set
waitMsandretriesexplicitly.
Troubleshooting
If the port is still in use after SIGTERM, retry with SIGKILL or use
elevated permissions:
sudo npx clean-port 3000 --signal SIGKILL