Node.js Port 3000 already in use but it actually isn't?
I have been working with a node.js project for a few weeks and it has been working great. Usually, I use npm start
to run my app and view it in a browser on localhost, port 3000.
Today, I started to get the following error while using npm start:
Server started on port 3000
Port 3000 is already in use
I have checked the resource monitor and I have no other process running on port 3000. Why would I be getting this error message?
In my app.js I have the following code to set the port...is this incorrect? It worked fine before so I'm not sure what I am doing wrong.
// Set Port
app.set('port', (process.env.PORT || 3000));
app.listen(app.get('port'), function() {
console.log('Server started on port '+app.get('port'));
});
Thanks for the help!
EDIT:
I have tried running netstat and TCPView to check what process is using the port, but there is nothing using that port. I also tried restarting my laptop but I still get the same error.
You can search on how to kill that process.
For Linux/Mac OS search (sudo) run
this in the terminal:
$ lsof -i tcp:3000
$ kill -9 PID
On Windows:
netstat -ano | findstr :3000
tskill typeyourPIDhere
change tskill
for taskkill
in git bash
Maybe you can take this as reference. This single command line can kill the process running on given port.
npx kill-port 3000
To kill multiple ports.
npx kill-port 3000 8080 4200
Sometimes it happens, as @sova proposed This happens to me sometimes, EADDR in use. Typically there is a terminal window hiding out in the background that is still running the app. And that's also right with me.
It happens, when you have opened terminal for long time, yeah you have right, you have stop the process. But sometimes it didn't stop in the background. So best solution is that you close the terminal and start it again. It will solves your problem. becuase in my case it works.
Also,
sudo lsof -i:<PORT_NO>
close the instance for present time but unable to stop the process in background. So for one time,
sudo kill <PID>
works, but again when we update our code and save, this problem occurs again as with Nodemon.
So exit the terminal will solve the problem. OR
killall -9 node
I had the same problem. (The below steps work fine on Windows 10):
- Open Task manager (press Ctrl+Alt+Delete)
- Select the 'Processes tab'
- Search for 'Node.js: Server-side JavaScript'
- Select it and click on 'End task' button
Now you can run npm start
.
Hope it helps you.
I also encountered the same issue. The best way to resolve is (for windows):
-
Go to the Task Manager.
-
Scroll and find a task process named. Node.js: Server-side JavaScript
-
End this particular task.
There you go! Now do npm start and it will work as before!