TL;DR - Quick Fix
Just run this in your terminal:
Ever encountered the dreaded Error: listen EADDRINUSE: address already in use :::3000
when trying to start your Next.js development server? Here's a quick solution to add to your package.json
scripts.
The Problem
When developing with Next.js, sometimes the development server doesn't shut down properly, leaving port 3000 occupied. This commonly happens when:
- Your terminal crashes unexpectedly
- You force-quit the development process
- A previous session is still running in the background
The Solution
Add this script to your package.json
:
Now you can simply run:
How It Works
Let's break down the command:
lsof -t -i tcp:3000
: Lists process IDs using port 3000xargs kill
: Takes those IDs and kills the processes
When You Need This
This command is particularly helpful when:
- Switching between multiple Next.js projects
- After system sleep/wake cycles
- When your development environment crashes
- Hot reloading stops working
Add it to your development toolkit, and you'll never have to manually hunt down and kill those pesky port processes again!
Note: This command works on Unix-based systems (macOS, Linux). For Windows, you'll need a different approach using netstat
and taskkill
.