Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Port 3000 is already in use [nodemon] app crashed - waiting for file changes before starting [closed]

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

like image 726
Afeesudheen Avatar asked Oct 29 '19 10:10

Afeesudheen


People also ask

How do I fix error of Nodemon app crashed?

Reasons & Solutions: Maybe your PC running several processes in the Background. So you need to stop all the node process that are running. and then restart nodemon.

Does Nodemon restart on crash?

Nodemon monitors your source files for changes during development and reload on change. That's why it says 'waiting for file change' after a crash. In development, usually you made a mistake, correct it, save the source file, nodemon notices that and reloads.

How do I restart Nodemon?

js Automatic restart Node. js server with nodemon. In this case, if we make any changes to the project then we will have to restart the server by killing it using CTRL+C and then typing the same command again.


3 Answers

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
    
like image 76
Tudor Constantin Avatar answered Oct 22 '22 03:10

Tudor Constantin


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:

  • Node.js > Process > Signal Events
  • libuv Documentation

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:

  • Sample nodemon.json
  • Events
like image 40
stackunderflow Avatar answered Oct 22 '22 04:10

stackunderflow


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

fuser -n tcp -k 3000
like image 5
harshit kohli Avatar answered Oct 22 '22 03:10

harshit kohli