Quick Tip - Kill Stuck Next.js Development Server

2 min read
Next.jsDevelopmentTerminalTips
Abhay Ramesh
Abhay Ramesh
Full Stack Developer

TL;DR - Quick Fix
Just run this in your terminal:

lsof -t -i tcp:3000 | xargs kill

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:

{
  "scripts": {
    "kill": "lsof -t -i tcp:3000 | xargs kill"
  }
}

Now you can simply run:

npm run kill
# or
yarn kill
# or
pnpm kill

How It Works

Let's break down the command:

  1. lsof -t -i tcp:3000: Lists process IDs using port 3000
  2. xargs 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.