[node.js] Node / Express: EADDRINUSE, Address already in use - Kill server

I have a simple server running in node.js using connect:

var server = require('connect').createServer();
//actions...
server.listen(3000);

In my code I have actual handlers, but thats the basic idea. The problem I keep getting is

EADDRINUSE, Address already in use

I receive this error when running my application again after it previously crashed or errors. Since I am not opening a new instance of terminal I close out the process with ctr + z.

I am fairly certain all I have to do is close out the server or connection. I tried calling server.close() in process.on('exit', ...); with no luck.

This question is related to node.js

The answer is


delete undefined file in your project root directory (which created on app crash)


I found this solution, try it Give permission use sudo

  sudo pkill node

Reasons for this issues are:

  1. Any one application may be running on this port like Skype.
  2. Node may have crashed and port may not have been freed.
  3. You may have tried to start server more than one. To solve this problem, one can maintain a boolean to check whether server have been started or not. It should be started only if boolean return false or undefined;

I was using debugger and just not stopped the processes running with Ctrl+C. So when I wanted to start debugging I got this error.


Kill only node process that uses given PORT. As others mentioned, in bash you could use kill $(lsof -t -i:PORT)

Here is a Windows solution (there's a way!): netstat -bona | findstr ".0:PORT +0.0.0.0:0 +LISTENING" | for /f "tokens=5" %t in ('more') do taskkill /PID:%t /F. You need an elevated terminal for that (in VSCode you'll need to open itself with elevation, as integrated terminal cannot be elevated).

PS: if findstr not available, you can simply use find and replace " +" for spaces (non regex mode)


I hit this on my laptop running win8. this worked.

Run cmd.exe as 'Administrator':

C:\Windows\System32>taskkill /F /IM node.exe
SUCCESS: The process "node.exe" with PID 11008 has been terminated.

You may use hot-node to prevent your server from crashing/ run-time-errors. Hot-node automatically restarts the nodejs application for you whenever there is a change in the node program[source] / process[running node program].

Install hot-node using npm using the global option:

npm install -g hotnode


You may run into scenarios where even killing the thread or process won't actually terminate the app (this happens for me on Linux and Windows every once in a while). Sometimes you might already have an instance running that you didn't close.

As a result of those kinds of circumstances, I prefer to add to my package.json:

"scripts": {
    "stop-win": "Taskkill /IM node.exe /F",
    "stop-linux": "killall node"
},

I can then call them using:

npm run stop-win
npm run stop-Linux

You can get fancier and make those BIN commands with an argument flag if you want. You can also add those as commands to be executed within a try-catch clause.


Just in case check if you have added this line multiple times by mistake

app.listen(3000, function() {
  console.log('listening on 3000')
});

The above code is for express but just check if you are trying to use the same port twice in your code.


For all who came to this post and tried all may probably using nodemon or forever to do this. For example if you run PORT=6060 Nodemon You might get the same error that port 6060 is already in use.

If this is the case just run your project without nodemon if you really need to define port during run. Alternatively you can define port in your file itself if you wanna stick to nodemon.

For me I do this now PORT=6060 node app.js


server.close() takes a while to close the connection, thus we should make this an asynchronous call as such:

await server.close();

IMPORTANT: when using await, we must use the async keyword in our encapsulating function as such:

async () => {
  await server.close();
}

Use the below command in the terminal/cmd to change the port(npm run dev is for node.js) you may have other commands to run your app most of them will work while changing the port, easier and faster. Furthermore, you can use any port number that is free in your system instead of 3002

PORT=3002 npm run dev

Most of the times when one runs the project while exiting one abruptly or unknowingly presses control + z that gives you exit out of the port always go for control + c that won't exit from port to run the server or project.

Furthermore, its time to change the port number in your code

server.listen(3002);

You can also go the command line route:

ps aux | grep node

to get the process ids.

Then:

kill -9 PID

Doing the -9 on kill sends a SIGKILL (instead of a SIGTERM). SIGTERM has been ignored by node for me sometimes.


Rewriting @Gerard 's comment in my answer:

Try pkill nodejs or pkill node if on UNIX-like OS.

This will kill the process running the node server running on any port. Worked for me.


I was facing the same problem. I just changed my port number from 8000 to 6000. as you have 3000 you try 5000,4000,7000,8000 etc.


I wonder, why nobody yet mentioned this possibility:

  • If you provide ::listen(port) with string (intentionally or not), which wouldn't be a valid port number representation, it then can be internally converted to port number -1, ant then engine will try to connect to that -1 port, which then yields the same EADDRINUSE error, which in turn might be slightly confusing and turn you in the wrong direction for error fix search (hi, me xD).

So, debug your code and check what exactly you pass to functions, before starting to check for the processes, that are using your port.


This command list the tasks related to the 'node' and have each of them terminated.

kill -9  $( ps -ae | grep 'node' | awk '{print $1}')

In windows users: open task manager and end task the nodejs.exe file, It works fine.


For windows open Task Manager and find node.exe processes. Kill all of them with End Task.

enter image description here


I was getting this error once and took many of the approaches here.

My issues was that I had two app.listen(3000); calls in the same app.js script. The first app.listen() succeeded where the second threw the error.

Another useful command I came across that helped me debug was sudo fuser -k 3000/tcp which will kill any rogue processes you might have started (some processes may restart, e.g. if run with forever.js, but it was useful for me).


This means you have two node servers running on the same port ,if one is running on port let say 3000 change the other one to another port lets say 3001 and everything will work well


bash$ sudo netstat -ltnp | grep -w ':3000'
 - tcp6    0      0 :::4000      :::*        LISTEN      31157/node      

bash$ kill 31157

ps aux | grep node
kill -9 [PID] (provided by above command)

Description:


  1. ps will give the process status, aux provide the list of a: all users processes, u: user own processes, x: all other processes not attached to terminal.
  2. pipe symbol: | will pass the result of ps aux to manipulate further.
  3. grep will search the string provided(node in our case) from the list provided by ps aux.

First, you would want to know which process is using port 3000

sudo lsof -i :3000

this will list all PID listening on this port, once you have the PID you can terminate it with the following:

kill -9 {PID}

PowerShell users:

Taskkill /IM node.exe /F


FYI, you can kill the process in one command sudo fuser -k 3000/tcp. This can be done for all other ports like 8000, 8080 or 9000 which are commonly used for development.


Task Manager (ctrl+alt+del) ->

Processes tab ->

select the "node.exe" process and hit "End Process"


Check the PID i.e. id of process running on port 3000 with below command :

lsof -i tcp:3000

It would output something like following:

COMMAND  PID   USER   FD   TYPE  DEVICE  SIZE/OFF NODE NAME
node     5805  xyz    12u  IPv6  63135    0t0     TCP  *:3000 (LISTEN)

Now kill the process using :

kill -9 5805

With due respect to all the answers in the form, I would like to add a point.

I found that when I terminate a node app on error using Ctrl + Z, the very next time when I try to open it got the same error EADDRINUSE.

When I use Ctrl + C to terminate a node app, the next time I opened it, it did without a hitch.

Changing the port number to something other than the one in error solved the issue.


For Visual Studio Noobs like me

You may be running the process in other terminals!

After closing the terminal in Visual Studio, the terminal just disappears.

I manually created a new one thinking that the previous one was destroyed. In reality, every time I was clicking on New Terminal I was actually creating a new one on top of the previous ones.

So I located the first terminal and... Voila, I was running the server there.

multiple terminals withot realizying it


Linux

Run ps and determine the PID of your node process.

Then, run sudo kill PID

Windows

Use tasklist to display the list of running processes:

tasklist /O

Then, kill the node process like so (using the PID obtained from the tasklist command):

taskkill /pid PID

Here is a one liner (replace 3000 with a port or a config variable):

kill $(lsof -t -i:3000)

Node is running somewhere in memory and has that port locked down. On Windows, this problem will happen, like most Windows problems, be solved by hitting CTRL+ALT+DEL and/or rebooting.


On Linux.

Add function to ~/.bashrc:

function killTcpListen () {
  kill -9 $(lsof -sTCP:LISTEN -i:$1 -t)
}

Pull changes: source ~/.bashrc

And use it: killTcpListen 3000


Win10, git bash v2.15, node v8.9.1, npm v5.5.1

I had a package.json script to start node: "start": "node index.js"

Whenever I used this, regardless if I killed it with ctrl+c, I ran into this issue.

If I just ran node index.js from git bash instead of npm run start and killed with ctrl+c, I never got this error.

I'm not sure as to why, but I figured this might help someone.


In my case, it was because I have Eclipse open...


First find out what is running using:

sudo lsof -nP -i4TCP:3000 | grep LISTEN

You will get something like:

php-fpm 110 root    6u  IPv4 0x110e2ba1cc64b26d      0t0  TCP 127.0.0.1:3000 (LISTEN)
php-fpm 274 _www    0u  IPv4 0x110e2ba1cc64b26d      0t0  TCP 127.0.0.1:3000 (LISTEN)
php-fpm 275 _www    0u  IPv4 0x110e2ba1cc64b26d      0t0  TCP 127.0.0.1:3000 (LISTEN)

Then you can kill the process as followed:

sudo kill 110

Then you will be able to run without getting the listen EADDRINUSE :::3000 errors


On Windows, I was getting the following error:

EADDRINUSE: address already in use :::8081.

Followed these steps:

  • Opened CMD as Admin
  • Ran the folowing

command netstat -ano|findstr "PID :8081"

got the following processes:

enter image description here

killed it via:

taskkill /pid 43144 /f

enter image description here

On MAC you can do like this:

raghavkhunger@MacBook-Air ~ % lsof -i tcp:8081 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME node 23722 username 24u IPv6 0xeed16d7ccfdd347 0t0 TCP *:sunproxyadmin (LISTEN)

username@MacBook-Air ~ % kill -9 23722


UI solution For Windows users: I found that the top answers did not work for me, they seemed to be commands for Mac or Linux users. I found a simple solution that didn't require any commands to remember: open Task Manager (ctrl+shift+esc). Look at background processes running. Find anything Node.js and end the task.

After I did this the issue went away for me. As stated in other answers it's background processes that are still running because an error was previously encountered and the regular exit/clean up functions didn't get called, so one way to kill them is to find the process in Task Manager and kill it there. If you ran the process from a terminal/powerShell you can usually use ctrl+c to kill it.


I had the same problem and I found out that it was the nodemon problem. First I was using this script to start my process:

{"dev": "nodemon -r dotenv/config app.js"}

the app boots correctly, but as soon as any file changes, nodemon can't restart it. In the meantime, the app still continues to run in the background. If I do Ctrl+C, it quits, but there's no more process on port 3000, so killing it by port fuser -k 3000/tcp doesn't do anything.

And, I was using .env port in app.js file.

const port = process.env.PORT || 3000;

So, I changed the port value to 3000 only and it worked.

const port = 3000;

I had to find another way to load .env file, but this solved the issue for me. Hope that helps.


In Linux try

pkill nodejs 
//or 
pkill node