The Problem
When running next dev
or npm run dev
, you might encounter this warning:
âš Port 3000 is in use, trying 3001 instead.
Why This Happens
This issue occurs when the Next.js development server doesn’t shut down properly, leaving port 3000 occupied. Common scenarios include:
- Terminal crashes unexpectedly
- Force-quitting the development process
- Previous session still running in background
Quick Solutions
Use either of these commands to kill the process using port 3000:
Method 1: Using lsof (macOS/Linux)
lsof -t -i tcp:3000 | xargs kill
Method 2: For Linux systems without lsof
kill $(ss -lptn 'sport = :3000' | awk 'NR>1 {print $NF}' | sed -E 's/.*pid=([0-9]+).*/\1/')
How These Commands Work
Method 1 breakdown:
lsof -t -i tcp:3000
– Lists process IDs using TCP port 3000xargs kill
– Kills the found processes
Method 2 breakdown:
ss -lptn 'sport = :3000'
– Shows socket statistics for port 3000awk 'NR>1 {print $NF}'
– Extracts the last field from each line (skipping header)sed -E 's/.*pid=([0-9]+).*/\1/'
– Extracts the process ID from the formatkill $(...)
– Kills the extracted process ID
After running either command, you can start your Next.js development server normally on port 3000.