Guide
EADDRINUSE: address already in use across git worktrees
Facts checked
Every git worktree is a full copy of your project, including the dev script that says “listen on 3000.” The first worktree gets the port; the next one fails with EADDRINUSE: address already in use.
The fix is to give each worktree its own port through an environment variable, and make every server read it. Below: how to find what holds a port, four ways to assign them, and how Dunea Code does it for you.
The error
Start the app in a second worktree while the first is running and you get some version of this:
Error: listen EADDRINUSE: address already in use :::3000
code: 'EADDRINUSE',
errno: -48,
syscall: 'listen',
address: '::',
port: 3000Other stacks say it differently: “Address already in use (Errno 48)” in Python, “Address already in use - bind(2)” in Ruby, “bind: address already in use” in Go. Same cause.
Why worktrees cause it
git worktree add gives you a second working folder of the same repo. Every tracked file comes with it, including package.json, vite.config.ts, docker-compose.yml and any script with a port in it. So two worktrees running pnpm dev run the identical command.
It’s rarely only one port. A typical project binds several, and each can clash:
- the web dev server (3000, 5173)
- a separate API (4000, 8000, 8080)
- the HMR websocket, if configured on its own port
- Storybook (6006), a docs site, a worker’s health endpoint
- databases started by
docker compose(5432, 6379)
This comes up most when running several AI coding agents at once, each in its own worktree. See how to run multiple Claude Code agents in parallel for the full setup.
Find what is holding the port
Before killing anything, check what it is. It may be another worktree you still want.
lsof -nP -iTCP:3000 -sTCP:LISTEN
# COMMAND PID USER FD TYPE ... NAME
# node 41872 you 23u IPv6 ... *:3000 (LISTEN)
ps -o pid,command -p 41872 # what is it, and which folder?
lsof -p 41872 | grep cwd # its working directory = which worktree
kill 41872Get-NetTCPConnection -LocalPort 3000 -State Listen | Select-Object OwningProcess
Get-Process -Id 41872
Stop-Process -Id 41872netstat -ano | findstr :3000
taskkill /PID 41872 /FFix 1: a PORT per worktree
Pick a different port in each worktree and pass it in. Most frameworks read PORT or take a flag:
PORT=3010 pnpm dev # Next.js, Remix, Express apps that read PORT
pnpm dev --port 3010 --strictPort # Vite, and SvelteKit (runs on Vite)
bin/rails server -p 3010 # Rails
python manage.py runserver 3010 # Django
uvicorn app:app --reload --port 3010 # FastAPI# PowerShell
$env:PORT = 3010; npm run dev
# cmd
set PORT=3010&& npm run dev
# any shell, and in package.json scripts
npx cross-env PORT=3010 npm run devYour own servers need to read it rather than hardcode it:
const port = Number(process.env.PORT ?? 3000);
app.listen(port, () => console.log(`listening on ${port}`));import { defineConfig } from 'vite';
export default defineConfig({
server: { port: Number(process.env.PORT ?? 5173), strictPort: true },
});Fix 2: one base port, offsets for each service
With several services, give each worktree a base and derive the rest. Space the bases 10 apart so worktrees never overlap:
#!/usr/bin/env bash
set -euo pipefail
BASE="${PORT_BASE:-3000}"
export WEB_PORT=$BASE
export API_PORT=$((BASE + 1))
export STORYBOOK_PORT=$((BASE + 2))
export NEXT_PUBLIC_API_URL="http://localhost:$API_PORT"
pnpm --filter api dev --port "$API_PORT" &
pnpm storybook --port "$STORYBOOK_PORT" --no-open &
PORT=$WEB_PORT pnpm --filter web dev
waitWorktree one runs PORT_BASE=3000 ./dev.sh, worktree two PORT_BASE=3010 ./dev.sh. For Docker services, use the same variables in docker-compose.yml, for example "${DB_PORT:-5432}:5432", and set a different COMPOSE_PROJECT_NAME per worktree so the containers don’t collide either.
Fix 3: direnv, so you never type the port
direnv loads a folder’s .envrc when you cd into it. Commit one .envrc that derives a base port from the worktree’s path, and each worktree computes its own:
# a stable port block per worktree folder, 3000 to 3990
slot=$(( $(printf '%s' "$PWD" | cksum | cut -d' ' -f1) % 100 ))
export PORT_BASE=$(( 3000 + slot * 10 ))
export PORT=$PORT_BASE
export API_PORT=$(( PORT_BASE + 1 ))
dotenv_if_exists .env.localdirenv allowTwo folders can hash to the same slot (with 100 slots, it gets likely past about a dozen worktrees). When that happens, override it in that worktree’s .env.local. direnv works on macOS and Linux; on Windows it needs WSL or Git Bash.
Fix 4: let the dev server pick a free port
Vite moves to the next free port by default when its port is taken (unless strictPort is set), and some other tools do the same. That stops the crash, but the port now changes from run to run. Anything that points at a fixed port breaks: the frontend’s API URL, OAuth callback URLs, a webhook tunnel, a bookmarked tab, or an agent told to test on localhost:5173. Fine for one service; for more, use fixes 1 to 3.
The Dunea Code way
Dunea Code is a free desktop app for running Claude Code and Codex agents in parallel on macOS, Windows and Linux. Every task gets its own branch and git worktree, and every worktree leases its own block of ports:
- 10 ports per worktree, from the range 3000 to 9999, as
$DUNEA_PORTand$DUNEA_PORT_0to$DUNEA_PORT_9, plus$DUNEA_PORT_COUNT. - Checked against your machine. Each port in a block has to be free (it can bind, and nothing answers on it) before the block is leased, so a database or another app already on 3000 is skipped.
- Known bad ports skipped. 5000 and 7000 (taken by macOS AirPlay Receiver) and ports browsers refuse to load.
- No overlap. Blocks are aligned and leased one at a time, so two new worktrees never get the same ports.
- Env files too. Gitignored files like
.envare copied into each new worktree.
Your run script reads the variables, the same as fix 2 with the base handed to you:
#!/usr/bin/env bash
set -euo pipefail
export API_PORT="$DUNEA_PORT_1"
export NEXT_PUBLIC_API_URL="http://localhost:$API_PORT"
pnpm --filter api dev --port "$API_PORT" &
export PORT="$DUNEA_PORT"
exec pnpm --filter web devDon’t want to write it? Ask an agent in Dunea Code to set up the repo; the built-in dunea-setup skill writes .dunea/setup.sh and .dunea/run.sh for your stack. Comparing tools? See how port handling differs in Conductor, Superset and the Claude Code desktop app.
FAQ
What does EADDRINUSE mean?
EADDRINUSE means “address already in use”: a process tried to listen on a port that another process is already listening on. Only one process can listen on a given address and port at a time.
Why do I get EADDRINUSE only when using git worktrees?
Each worktree is a full copy of your project with the same scripts and config, so every worktree’s dev server tries the same hardcoded port. The first one gets it and the rest fail. Worktrees share your files’ history, but they also share your machine’s ports.
How do I kill the process using port 3000?
On macOS or Linux, run lsof -nP -iTCP:3000 -sTCP:LISTEN to get the PID, then kill <PID>. On Windows, run netstat -ano | findstr :3000 and then taskkill /PID <PID> /F. Check what it is first: it may be another worktree you still want running.
Can I put PORT in .env for Next.js?
Not reliably. next dev picks its port before it loads .env files, so set PORT in the shell or with -p. Many other tools read .env early enough, but passing the port on the command line always works.
Does Dunea Code fix port conflicts automatically?
Each worktree in Dunea Code leases a block of 10 ports that were free on your machine, exposed as $DUNEA_PORT and $DUNEA_PORT_0 to $DUNEA_PORT_9. Your run script passes them to the dev server, and no two worktrees get overlapping blocks.
Try Dunea Code on your own repo
Free on macOS, Windows and Linux. Bring your Claude Code or Codex plan; neither uses Dunea credits. See what Dunea Code does or read the docs.
v0.2.43 · Apple Silicon · Free · Other platforms