Port 3000 is already in use [nodemon] app crashed - waiting for file changes before starting... so please help me to solve this
Port 3000 is already in use [nodemon] app crashed - waiting for file changes before starting... so please help me to solve this
Borrowed from my answer to this question:
Install the kill-port node package as a dev dependency:
npm install kill-port --save-dev
Create a nodemon.json file in the root of your project containing:
{
"events": {
"restart": "kill-port 3000",
"crash": "kill-port 3000"
},
"delay": "1500"
}
Then, in your package.json file, have something like this:
"scripts": {
"start-dev": "nodemon app.js",
}
Then start your app in dev mode with:
npm run start-dev
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
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 behaviourIf your app listens in on
SIGTERMto shutdown gracefully when nodemon triggerstskillcommand, Windows will unconditionally terminate your process before your app has a chance to fire theprocess.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
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:
Best Way to counter this problem is directly kill the port with the following command.
fuser -n tcp -k 3000
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.
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).
It seems that another process is using the port. You might wanna try to kill the process running on port 3000 by using the solutions here depending on your OS.
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.