10

Port 3000 is already in use [nodemon] app crashed - waiting for file changes before starting... so please help me to solve this

Afeesudheen
  • 936
  • 2
  • 12
  • 21

6 Answers6

31

Killing a process that owns port 3000

Unix-like Operating Systems (e.g. GNU/Linux, FreeBSD, macOS)

First, let’s take a look at how we can kill a process that has a port open.

Using the lsof command, we can retrieve the PID that has the given port:

$ lsof -i :3000 -t
12345

Then we can kill this process just by doing:

$ kill 12345

Let’s turn this into a one-liner:

lsof -i :3000 -t | xargs kill

If you’re using environment variable to set the server port, we can specify that instead of hardcoding our values:

lsof -i :${PORT} -t | xargs kill

Lastly, we can default to port 3000 if environment variable isn’t set:

lsof -i :${PORT:-3000} -t | xargs kill

Microsoft Windows

Unless you’re running nodemon on Windows Subsystem for Linux (WSL), lsof is not available in Windows. However, netstat is available on Windows shell:

netstat -ano | findstr :3000

This will return the PID of the process that is using up port 3000 which we can use to kill the process using tskill command:

tskill 12345

If all you care about is making sure the process that owns the port is dead without any graceful shutdown, you can disregard the caveat below.

Caveat on Windows process kill behaviour

If your app listens in on SIGTERM to shutdown gracefully when nodemon triggers tskill command, Windows will unconditionally terminate your process before your app has a chance to fire the process.on('SIGTERM') event handler.

More details on this caveat are here:

Sometimes the tskill command won't run due to some reasons. You can also use the following command for killing the process after finding the PID from the above netstat command

taskkill /F /T /PID 12345

Getting nodemon to execute hooks

Nodemon lets you set up event hooks through nodemon.json configuration file:

{
  "events": {
    "crash": "sh -c 'lsof -i :${PORT:-3000} -t | xargs kill'"
  }
}

This will cause nodemon to execute sh -c 'lsof -i :${PORT:-3000} -t | xargs kill command whenever your app crashes, thereby killing the child process it spawned that’s keeping the port open.

For more info on nodemon events, checkout their documentation:

Rahul Soshte
  • 798
  • 8
  • 11
stackunderflow
  • 546
  • 3
  • 7
31

Borrowed from my answer to this question:

  1. Install the kill-port node package as a dev dependency:

    npm install kill-port --save-dev
    
  2. Create a nodemon.json file in the root of your project containing:

    {
      "events": {
        "restart": "kill-port 3000",
        "crash": "kill-port 3000"
      },
      "delay": "1500"
    }
    
  3. Then, in your package.json file, have something like this:

    "scripts": {
        "start-dev": "nodemon app.js",
    }
    
  4. Then start your app in dev mode with:

    npm run start-dev
    
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72
5

Best Way to counter this problem is directly kill the port with the following command.

fuser -n tcp -k 3000
harshit kohli
  • 302
  • 2
  • 8
0

You can use the following command at terminal to check which process is using that port:

netstat -aon | findstr '[port_number]'

If that process is not required, you can kill that process and start your own process on port 3000. Otherwise you can always use another port for your application.

Most probably this process will be nodemon, which you can kill and start again.

Aman Kaushik
  • 360
  • 2
  • 10
0

I had a similar issue with nodemon continuing to watch for file changes even after I closed the terminal. I stopped all node processes (Not just the node process using the port).

sam
  • 1,005
  • 1
  • 11
  • 24
-2

It seems that 3000 port is in use. You can kill process that is using 3000 port or you can change port of node from 3000 to any another port.