steal-port: Reclaim Ports from Coding Agents
Sometimes coding agents like Claude Code leave a service running that you want to run yourself. steal-port is a small script you can add to your run command to easily reclaim a port and switch out an app or service for your own.
Usage
The tool supports three invocation patterns:
# Use port from .env.local or .env file
steal-port
# Specify port as positional argument
steal-port 3000
# Named parameter formats
steal-port --port 3000
steal-port -p 3000
How It Works
- Checks for an explicitly provided port argument first
- Searches for
PORT=variable in.env.localfile - Falls back to checking
.envfile if.env.locallacks the port - Terminates with an error if no port configuration exists
- Identifies and terminates the process occupying that port
- Completes successfully regardless of whether a process required termination
Integration with npm Scripts
The most common pattern is to prefix your dev commands:
{
"scripts": {
"dev": "steal-port && vite",
"watch": "steal-port && nest start --watch"
}
}
This ensures the port is free before launching your development server.