Port 3000 is already in use [nodemon] app crashed - waiting for file changes before starting... so please help me to solve this
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.
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.
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.
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
SIGTERM
to shutdown gracefully when nodemon triggerstskill
command, 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:
- 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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With