Node.js Port 3000 already in use but it actually isn't?

472,652

Solution 1

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

Solution 2

Maybe you can take this as reference. This single command line can kill the process running on given port.

npx kill-port 3000

enter image description here


To kill multiple ports.

npx kill-port 3000 8080 4200

Solution 3

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

Solution 4

I also encountered the same issue. The best way to resolve is (for windows):

  1. Go to the Task Manager.

  2. Scroll and find a task process named. Node.js: Server-side JavaScript Image added for reference

  3. End this particular task.

There you go! Now do npm start and it will work as before!

Solution 5

I had the same problem. (The below steps work fine on Windows 10):

  1. Open Task manager (press Ctrl+Alt+Delete)
  2. Select the 'Processes tab'
  3. Search for 'Node.js: Server-side JavaScript'
  4. Select it and click on 'End task' button

Now you can run npm start.

Hope it helps you.

Share:
472,652

Related videos on Youtube

user2573690
Author by

user2573690

Updated on August 01, 2022

Comments

  • user2573690
    user2573690 almost 2 years

    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.

    • tanaydin
      tanaydin over 7 years
      There is another process that uses this port, it is certain. Which os are you trying to ? You can google it like 'find which prosess uses port' for your operating system
    • Blorgbeard
      Blorgbeard over 7 years
      Have you tried browsing to localhost:3000 ?
    • user2573690
      user2573690 over 7 years
      @tanaydin I have checked Resource Monitor on Windows 10 and there is no process listening on port 3000. Unless its one that I cannot see?
    • Isaac
      Isaac over 7 years
    • user2573690
      user2573690 over 7 years
      @Blorgbeard nothing comes up, "Site can't be reached" however, on the Chrome tab I see the favicon of my node site...but the app itself isn't running
    • Blorgbeard
      Blorgbeard over 7 years
      The favicon will be cached. You could also try netstat in a command prompt, or connecting to localhost:3000 with a telnet equivalent - PuTTY, for example.
    • Blorgbeard
      Blorgbeard over 7 years
      And of course, this might work..
    • user2573690
      user2573690 over 7 years
      @Blorgbeard I tried TCPView and netstat and there is nothing running on port 3000, I also tried restarting my laptop and still the same issue.
    • Blorgbeard
      Blorgbeard over 7 years
      I notice you get "Port 3000 is already in use" after "Server started on port 3000" - is something in your app attempting to start listening again on the same port?
    • user2573690
      user2573690 over 7 years
      @Blorgbeard I checked the npm-debug log and I noticed that there is an error "Tell the author that this fails on your system: node ./bin/www"
    • Josh Beam
      Josh Beam over 7 years
    • Josh Beam
      Josh Beam over 7 years
      Marked as dupe, try: ps aux | grep node then kill -9 PID
    • jfriend00
      jfriend00 over 7 years
      My guess is that you have two app.listen() statements in your app on another .listen() that is also trying to start a server on that port. The first one works, the second one reports the error. Search your code for .listen.
    • Steve Bennett
      Steve Bennett over 7 years
      Sounds a lot like you have a bug in your start up code. Maybe add or link more of your code and someone will find it?
    • SMEETT
      SMEETT over 2 years
      For me it was my .env - so make sure there are no syntax errors there.
  • user2573690
    user2573690 over 7 years
    Thanks for the help! I don't have any other terminal windows open, anything else I should check?
  • sova
    sova over 7 years
    find any node or npm process and end it. if still you have a funk, reboot machine, or just pick a different port to work with. There's really no reason it must be port 3000 or 8080
  • user2573690
    user2573690 over 7 years
    I just created a new node app and started it on port 3000 and that one seems to work fine, but when I try to run my existing project, it says the port is in use. Have you ever had this issue?
  • sova
    sova over 7 years
    @user2573690 i have not come across that before, but maybe you have multiple js files (like an app.js and an index.js) where one is calling .listen() multiple times?
  • user2573690
    user2573690 over 7 years
    Thank you! I managed to figure it out, I was listening to the port multiple times, copy/pasta accident! If you can edit your answer and add that piece, I will mark it. Again, thank you!
  • Cody
    Cody almost 7 years
    Here's a weird issue: I've Ctrl+C'd (^C) out of the process in GitBash, but when I hoist the server again -- npm start -- then I get the EADDR in use. When I refresh my browser, I see all the GET logs that indicate the thread is still running. This usually happens if I've slept my laptop and come back. If I'm lucky, closing out all instances of GitBash and waiting a minute seems to do the trick, though, its a pain shutting down different instances across multiple git projects/servers.
  • Cody
    Cody almost 7 years
    [SEE ABOVE] I just had experiment: I shutdown (Ctrl+C) my server and waited about the same amount of time it takes me to shutdown all my GitBash instances before hoisting the server again -- that seems to have made the difference. Hope this saves you time.
  • Julsteri
    Julsteri about 6 years
    Neither the lsof or netstat returned anything, yet there still seemed to be some process using the port. After killall -9 node I was able to run the server locally.
  • truedat101
    truedat101 over 5 years
    I've tried a number of other solutions on windows, but this one found the odd process that was hogging the port. Earned an upvote for including also the linux approach in addition to working on windows.
  • snersesyan
    snersesyan over 5 years
    tskill didn't working for me on windows. taskkill /F /PID myPIDhere - this working
  • user985366
    user985366 almost 5 years
    I get nothing with only lsof but with sudo lsof I get something, and killing that process solved this problem.
  • Kalkhas
    Kalkhas almost 5 years
    Adjust the delay as required.
  • S_W
    S_W over 4 years
    Is there a way to dynamically get the PID for the running process and kill it? For some reason, I have to do this every time I deploy to prod manually. Side note, sure if this is related to PM2 or not.
  • Titou
    Titou over 4 years
    upvoted for the willingness to help AND a solution that may help find the root cause.
  • Nathan
    Nathan over 4 years
    I just had this happen to me as well. netstat -ano didn't list anything using port 3000.
  • 3Dos
    3Dos over 4 years
    Holy hell, this solved it for me too as obviously nothing was running on port 3000. I started having this issue after a Windows update. Never thought about turning WiFi off. Thank you for solving this :)
  • twknab
    twknab over 4 years
    This is quite a nice and clean solution to killing a process. Have to look how to do this up every time, and this is the nicest solution I've seen yet!
  • xaunlopez
    xaunlopez over 4 years
    Im experiencing this issue although no processes are returning from lsof -i :3000 -t =\
  • ifhy
    ifhy about 4 years
    thanks for the killall -9 node command. it worked on goorm IDE
  • nickcamillo
    nickcamillo about 4 years
    taskkill did not work for me on git-bash, but tskill did. thanks.
  • Afeesudheen
    Afeesudheen almost 4 years
    @xaunlopez try this one fuser -k port-number/tcp
  • Deep Roy
    Deep Roy almost 4 years
    worked for me just had to add the PID displayed from the "lsof -i tcp:3000" command
  • vibhor vaish
    vibhor vaish almost 4 years
    Would have even better if you had explained the commands in detail too but stackoverflow is life saving.
  • Hrishikesh Baidya
    Hrishikesh Baidya over 3 years
    Great solution Thanks a ton.
  • Bergi
    Bergi over 3 years
    What does kill-port do? How does it work?
  • Mohammad Hossein Ganjyar
    Mohammad Hossein Ganjyar over 3 years
    @Bergi It kill its port that you want run for app
  • Bergi
    Bergi over 3 years
    No, it doesn't. A port cannot be killed.
  • Mohammad Hossein Ganjyar
    Mohammad Hossein Ganjyar over 3 years
    @Bergi you should read its document, please.
  • Bergi
    Bergi over 3 years
    Please put the relevant parts in your answer as well.
  • Manuel Cheța
    Manuel Cheța over 3 years
    Awesome. Thanks.
  • Ben Alan
    Ben Alan over 3 years
    I killed the process, but another one immediately started with a different PID, also using port 3000
  • Amit Khanna
    Amit Khanna about 3 years
    npx kill-port 3000 worked for me. Thanks @Penny
  • Herman Van Der Blom
    Herman Van Der Blom about 3 years
    You are the Greatest :-) The line: "stop": "taskkill -f -im node.exe" in the package.json did it for me. I work remote with SSH. I had to look for a solution I think more as one Hour. All the kill commands etc. did not work.
  • Snowcat
    Snowcat almost 3 years
    I had the same error on Ubuntu so what helped me was 1) make sure you haven't already assigned Port to a variable and are still using quotes ("port") to get your port connection instead of the variable. Secondly, on Ubuntu you can use Run sudo netstat -lp to figure out what is using that port
  • shailesh gavathe
    shailesh gavathe almost 3 years
    for me this helped. I had turned on wifi at some point in the day. I usually use the Ethernet. So i completely cut off wifi and then rebooted it. Everything back to working again. Wow. Without your hint, I would have been wondering off for hours maybe.
  • Xairoo
    Xairoo almost 3 years
    net stop winnat - I just forgot that command. Needed it a few weeks ago and today because of the same problem (appears sometimes... why?!). Will add this to autostart.
  • towith
    towith almost 3 years
    Seems the winnat service is started by windows hyper-v
  • PDHide
    PDHide almost 3 years
    @MohammadHosseinGanjyar such a simple solution well done
  • Matt West
    Matt West almost 3 years
    I had no idea this package existed. Only 7:30am and my day is already made.
  • anderskristo
    anderskristo over 2 years
    Wow... this definitely saved my evening
  • hussamsindhu
    hussamsindhu over 2 years
    yeah It's working thanks, but what package it installed ?
  • Voxlinou
    Voxlinou over 2 years
    This does not fix the user's problem or help fixing it, please look on how to write a good answer
  • Eric Aya
    Eric Aya over 2 years
    This is the same solution as in this other answer.
  • Admin
    Admin over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
  • Matthew
    Matthew over 2 years
    This worked for me too. Then I tried WiFi on and VPN off...also worked. Anyone know why this might be?
  • Droid Chris
    Droid Chris about 2 years
    yep this works great!
  • Shannon Cole
    Shannon Cole about 2 years
    Awesome! Worked great. I love simple solutions.
  • TomDom
    TomDom about 2 years
    Thanks a lot. This solved my problem. I've been trying to find a solution for the last couple of hours.
  • Gary
    Gary about 2 years
    I am using netstat -ano | find "14228" and want to use this with taskkill /PID xxx. I am not able to pipe it with | nor with >. It is throwing up on me with a wrong format error. What am I missing? These are the two : netstat -ano | find "14228" | taskkill /PID xxx or netstat -ano | find "14228" > taskkill /PID xxx. I have tried this netstat -ano | find "14228" | ( set /P var= && set var ) > taskkill /PID ........
  • Gary
    Gary about 2 years
    I am using netstat -ano | find "14228" and want to use this with taskkill /PID xxx. I am not able to pipe it with | nor with >. It is throwing up on me with a wrong format error. What am I missing? These are the two : netstat -ano | find "14228" | taskkill /PID xxx or netstat -ano | find "14228" > taskkill /PID xxx. I have tried this netstat -ano | find "14228" | ( set /P var= && set var ) > taskkill /PID ........
  • Towerss
    Towerss about 2 years
    After trying all the previous solutions I got here. There was no visible process running on port 3000. This solution worked for me. Thank you.
  • May'Habit
    May'Habit almost 2 years
    sorry, where is the setting put?