Skip to content
FR
  1. LuzineDebug
  2. JavaScript & Node.js
  3. Error: listen EADDRINUSE: address already in use…
JavaScript & Node.js Beginner ≈ 5 min to fix

Fix "Error: listen EADDRINUSE: address already in use" in Node.js

Error message
Error: listen EADDRINUSE: address already in use :::3000
  • Error: listen EADDRINUSE: address already in use 127.0.0.1:3000
  • Error: listen EADDRINUSE: address already in use 0.0.0.0:8080
  • Error: listen EADDRINUSE :::3000
  • Error: Port 5173 is already in use
  • code: 'EADDRINUSE', errno: -98, syscall: 'listen', address: '::', port: 3000
In short

The port your Node.js server wants to listen on (here 3000) is already taken, most often by an old instance of your own server that is still running. Find the process using it (lsof -i :3000 on macOS/Linux, netstat -ano | findstr :3000 on Windows), stop it, or start the app on another port.

Be the first to confirm this fix

What this error means

When you call server.listen(3000) or app.listen(3000), Node.js asks the operating system to reserve that TCP port. A port can only be listened on by one process at a time on the same address. If it is already taken, the system returns the EADDRINUSE code and Node.js throws the error, then exits if it is not handled. :::3000 means "every IPv6 and IPv4 address, port 3000". The errno number depends on the OS (-98 on Linux, -48 on macOS, -4091 on Windows), but the cause is always the same: another process holds the port.

Common causes

  1. Old instance still running: server started in another terminal, orphaned nodemon process, or terminal closed without stopping the server.
  2. Process suspended with Ctrl+Z: on macOS and Linux, Ctrl+Z pauses the process without exiting, and it keeps the port.
  3. Another application on the same port: another project, a Docker container publishing that port, or a system service (on macOS, AirPlay Receiver uses ports 5000 and 7000).
  4. listen called twice in the code: for example in both app.js and server.js, or in every test file that imports the app.

Step-by-step fix

  1. Find the process holding the port

    Replace 3000 with your port. The command gives the PID (process id) and its name.

    bash
    # macOS / Linux
    lsof -i :3000
    
    # Linux (alternative)
    sudo ss -ltnp 'sport = :3000'
    
    # Windows (cmd or PowerShell)
    netstat -ano | findstr :3000
    tasklist /FI "PID eq 12345"
  2. Stop the process

    If it is an old instance of your server, stop it with its PID. Try a normal stop first, and only force it (-9, /F) if it does not respond. Check the process name first: do not kill a system service or someone else’s application.

    bash
    # macOS / Linux
    kill 12345
    kill -9 12345
    
    # Windows (cmd)
    taskkill /PID 12345 /F
    
    # Windows (PowerShell), by port
    Stop-Process -Id (Get-NetTCPConnection -LocalPort 3000 -State Listen).OwningProcess
  3. Resume a suspended process (macOS / Linux)

    If you pressed Ctrl+Z, the server is paused in the same terminal. List the jobs, bring it back to the foreground, then quit it with Ctrl+C.

    bash
    jobs
    fg %1
  4. Make the port configurable

    Read the port from an environment variable so you can change it without touching the code.

    javascript
    const port = Number(process.env.PORT) || 3000;
    app.listen(port, () => console.log(`Listening on http://localhost:${port}`));
    
    // macOS / Linux:     PORT=3001 npm start
    // Windows (PowerShell): $env:PORT=3001; npm start
  5. Call listen only once

    Export the app without starting the server, and call listen in a single entry file. Tests (for example with supertest) then import app without opening a port.

    javascript
    // app.js
    import express from 'express';
    export const app = express();
    
    // server.js
    import { app } from './app.js';
    app.listen(process.env.PORT || 3000);
  6. Handle the error cleanly

    Listen to the server’s error event to print a clear message instead of a stack trace.

    javascript
    const server = app.listen(port);
    
    server.on('error', (err) => {
      if (err.code === 'EADDRINUSE') {
        console.error(`Port ${port} is already in use. Stop the other process or set PORT.`);
        process.exit(1);
      }
      throw err;
    });

Did this fix work for you?

Confirm it in one click: it will rise for the next developers stuck on the same error.

Keep it from coming back

  • Always stop your server with Ctrl+C rather than Ctrl+Z or by closing the terminal window.
  • Use the PORT variable and a .env file per project so two local projects do not target the same port.
  • Handle SIGINT and SIGTERM to close the server (server.close()) cleanly, especially with nodemon and in Docker.

Frequently asked questions

Why is port 5000 taken on my Mac?

Since macOS Monterey, AirPlay Receiver listens on ports 5000 and 7000. Change your app’s port, or turn off "AirPlay Receiver" in System Settings > General > AirDrop & Handoff.

lsof returns nothing but the port is still taken, what now?

The process may belong to another user or to root: run sudo lsof -i :3000 again. Also check docker ps, since a container that publishes this port holds it on your machine.

How do I free a port in one command on macOS or Linux?

kill $(lsof -ti :3000) stops every process using port 3000. Check what they are first with lsof -i :3000.

Useful tools for this error

More JavaScript & Node.js errors

All JavaScript & Node.js errors →