How to fix Error: listen EADDRINUSE while using nodejs?

前端 未结 30 3196
执念已碎
执念已碎 2020-11-22 06:44

If I run a server with the port 80, and I try to use xmlHTTPrequest i get this error: Error: listen EADDRINUSE

Why is it problem for nodejs, if I want t

相关标签:
30条回答
  • 2020-11-22 06:58

    Error: listen EADDRINUSE means the port which you want to assign/bind to your application server is already in use. You can either assign another port to your application.

    Or if you want to assign the same port to the app. Then kill the application that is running at your desired port.

    For a node application what you can try is, find the process id for the node app by :

    ps -aux | grep node
    

    After getting the process id, do

    kill process_id
    
    0 讨论(0)
  • 2020-11-22 07:00

    Seems there is another Node ng serve process running. Check it by typing this in your console (Linux/Mac):

    ps aux|grep node
    

    and quit it with:

    kill -9 <NodeProcessId>
    

    OR alternativley use

    ng serve --port <AnotherFreePortNumber>
    

    to serve your project on a free port of you choice.

    0 讨论(0)
  • 2020-11-22 07:00

    Two servers can not listen on same port, so check out if other server listening on same port, also check out for browser sync if its running on same port

    0 讨论(0)
  • 2020-11-22 07:01

    On Debian i found out to run on port 80 you need to issue the command as root i.e

    sudo node app.js
    

    I hope it helps

    0 讨论(0)
  • 2020-11-22 07:02

    The option which is working for me :

    Run:

    ps -ax | grep node
    

    You'll get something like:

     8078 pts/7    Tl     0:01 node server.js
     8489 pts/10   S+     0:00 grep --color=auto node    
     kill -9 8078
    
    0 讨论(0)
  • 2020-11-22 07:03

    The aforementioned killall -9 node, suggested by Patrick works as expected and solves the problem but you may want to read the edit part of this very answer about why kill -9 may not be the best way to do it.

    On top of that you might want to target a single process rather than blindly killing all active processes.

    In that case, first get the process ID (PID) of the process running on that port (say 8888):

    lsof -i tcp:8888

    This will return something like:

    COMMAND   PID    USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
    node     57385   You   11u  IPv6 0xac745b2749fd2be3      0t0  TCP *:ddi-tcp-1 (LISTEN)
    

    Then just do (ps - actually do not. Please keep reading below):

    kill -9 57385

    You can read a bit more about this here.

    EDIT: I was reading on a fairly related topic today and stumbled upon this interesting thread on why should i not kill -9 a process.

    Generally, you should use kill -15 before kill -9 to give the target process a chance to clean up after itself. (Processes can't catch or ignore SIGKILL, but they can and often do catch SIGTERM.) If you don't give the process a chance to finish what it's doing and clean up, it may leave corrupted files (or other state) around that it won't be able to understand once restarted.

    So, as stated you should better kill the above process with:

    kill -15 57385

    EDIT 2: As noted in a comment around here many times this error is a consequence of not exiting a process gracefully. That means, a lot of people exit a node command (or any other) using CTRL+Z. The correct way of stopping a running process is issuing the CTRL+C command which performs a clean exit.

    Exiting a process the right way will free up that port while shutting down. This will allow you to restart the process without going through the trouble of killing it yourself before being able to re-run it again.

    0 讨论(0)
提交回复
热议问题