Library to clear a TCP port by terminating the process that uses it.
npm install clean-portClear a TCP port by terminating the process that currently owns it. Works on
macOS, Linux, and Windows.
- macOS or Linux: lsof must be available
- Windows: netstat must be available (included with Windows)
``bash`
npm install clean-port -g
`js
const { clearPort } = require("clean-port");
const result = clearPort(3000);
console.log(result);
`
Force cleanup if the port remains in use:
`js
const { clearPort } = require("clean-port");
const result = clearPort(3000, { force: true });
console.log(result);
`
`bash`
npx clean-port 3000
npx clean-port 3000 --force
npx clean-port 3000 --signal SIGKILL
- -s, --signal kill signal (default SIGTERM)-f
- , --force use SIGKILL if still in use--wait-ms
- delay between checks (default 300)--retries
- number of rechecks (default 5)
clearPort(port, options)
- port number or numeric stringoptions.signal
- kill signal (default SIGTERM)options.waitMs
- delay between checks in ms (default 300, non-negative)options.retries
- number of rechecks (default 5, non-negative integer)options.force
- use SIGKILL if still in use (default false)
Returns:
{ cleared: boolean, status: "cleared" | "force_cleared" | "already_clear" | "still_in_use", pids: number[], remainingPids: number[] }
- Start with SIGTERM and allow a short wait before retrying.force
- Use only when you can safely terminate the process.sudo
- Run as the same user that owns the process; use only when required.waitMs
- Avoid auto-restart supervisors while cleaning a port in dev scripts.
- For CI/automation, set and retries explicitly.
If the port is still in use after SIGTERM, retry with SIGKILL or use
elevated permissions:
`bash``
sudo npx clean-port 3000 --signal SIGKILL