553

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.

Skawful
  • 6,339
  • 4
  • 18
  • 19
  • 46
    Actually, instead of `Ctrl + z` you should use `Ctrl + c` which will close the program correctly by sending SIGQUIT :) See the [wiki](http://en.wikipedia.org/wiki/Signal_(computing)) for further details :) – nacho4d Nov 14 '11 at 08:14
  • 1
    You mean SIGINT. SIGQUIT is due to `ctrl + \\` – Xedecimal Feb 27 '12 at 01:51
  • use `server.close()` for previuos servers – Vladimir Starkov Jan 25 '13 at 04:49
  • 73
    Try `pkill nodejs` or `pkill node` if on UNIX-like OS – Gerard Nov 20 '13 at 21:24
  • 2
    I had a similar issue and found this package that will allow you to exit cleanly when you CTRL+C: https://www.npmjs.com/package/exit-hook – Jazzy Jul 31 '17 at 17:55
  • 1
    1. Seems like there is a dangling or Dead process ID latched on to the port, So the Node Based service is not starting and throwing error PORT IN USE (ERROR ADDRESS IN USE) 2. We are trying to find out how to release the port 10000 without rebooting the server. – abksharma Oct 11 '18 at 08:00
  • I had a kind of similar issue whatever port I set it was showing port already in use ,a simple restart for your desktop/computer solved my problem.hope it helps someone – Mahesh Jamdade Sep 13 '19 at 08:27
  • 1
    restarting your computer is a terrible solution. – Chris Hawkes Feb 17 '20 at 19:44
  • As @nacho4d said, you should use `ctrl + c` to close the program. But if you already did `ctrl + z`, you can use `fg` to focus on the program again, or use `bg` to let it resume in the background. Here is a better explanation: https://superuser.com/a/169057 – Charlon Aug 13 '20 at 17:44
  • @Gerard nice, worked like charm. – sg28 Dec 08 '20 at 21:05

45 Answers45

560

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.

djburdick
  • 10,532
  • 9
  • 39
  • 62
  • What really solved it for me is doing two things. 1. Running my server as a daemon / job. 2. when running within bash, killing the process with ctrl + c. The odd error sometimes causes the app to crash unexpectedly, then I usally 'kill ' – Skawful Nov 22 '10 at 19:32
  • 1
    `ps aux | grep node` shows nothing; still `textareaserver --editor-cmd='gvim -f'` fails: 14 Mar 21:19:30 - socket.io ready - accepting connections Could now start the server: EADDRINUSE, Address already in use – Jean Jordaan Mar 14 '11 at 14:21
  • 105
    Why this rather than `killall -9 node` – Martin Josefsson Nov 27 '13 at 10:43
  • 28
    I used this answer for a long time, then one day composed it into a one liner for convenience.. this command will kill any process running on a given port (8000 in this example): `lsof -n -i4TCP:8000 | grep LISTEN | tr -s ' ' | cut -f 2 -d ' ' | xargs kill -9` – lukejacksonn Jan 11 '16 at 16:57
  • 1
    I had multiple node servers running at once, some of them Electron apps. I had to use just `kill` with the specific process id instead of `killall`. – tuliomir Jan 05 '19 at 22:20
  • 1
    Just as a PSA: be very careful with what you kill since many services use Electron and you might inadvertently kill an unrelated process. – Maksim Nov 19 '19 at 09:08
  • Don't use `kill -9` unless you know what you are doing. Basically, try `kill PID` and `kill -2 PID` and `kill -15 PID` and wait a while, and if the process is still around, *then* bring out the big gun. Killing a process unconditionally doesn't let it do any cleanup, which could lead to file handle leaks and race conditions etc. Or cause the "address already in use" for that matter. – tripleee Jan 15 '21 at 05:47
378

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}
Mina Gabriel
  • 17,138
  • 23
  • 89
  • 118
  • 27
    This command clearly identifies the PID unlike the output of `ps aux | grep node` for me. I also did not need `sudo` – Phil Gibbins Jan 15 '18 at 14:20
  • 2
    This is the best answer. Worked fine for me with port 8081, using React Native with yarn. – Fernando Barbosa Mar 04 '20 at 18:51
  • This worked great for me. I could identify where the port 3000 was used and so close it. I also did not need `sudo`. – Campalo Mar 12 '20 at 15:16
  • Very well. I also tried after getting the command `killall -9 {COMMAND}` e.g. `killall -9 node` – STREET MONEY Apr 22 '20 at 07:38
  • The command to find a process using a specific port is very useful. So glad you posted this. – Caleb Thornton Nov 06 '20 at 14:44
  • Don't use `kill -9` unless you know what you are doing. Basically, try `kill PID` and `kill -2 PID` and `kill -15 PID` and wait a while, and if the process is still around, *then* bring out the big gun. Killing a process unconditionally doesn't let it do any cleanup, which could lead to file handle leaks and race conditions etc. Or cause the "address already in use" for that matter. – tripleee Jan 15 '21 at 05:45
  • To find a process on a port in Windows, refer to this thread https://stackoverflow.com/questions/48198/how-can-you-find-out-which-process-is-listening-on-a-tcp-or-udp-port-on-windows – Prashanth Subramanian May 10 '21 at 07:46
205

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.
rsjaffe
  • 5,050
  • 7
  • 23
  • 35
Sushil
  • 4,356
  • 2
  • 15
  • 15
  • I was running it from the Command Window and closed it by accident. Node kept running in the back ground... (even after the session was terminated). In my case once I closed the browser tab that was connected to it via web-sockets it finally terminated. – Bertus Kruger May 11 '15 at 04:28
  • 14
    `taskkill /F /IM node.exe` works like a charm for me on Windows from any directory :-) Thanks for the share !! – Marty McGee Jun 23 '15 at 23:36
  • 4
    This is the only single-line working solution I've been able to verify for windows – Code Whisperer Sep 06 '16 at 21:12
  • 6
    Works on windows 10. I didn't have to run the cmd.exe as admin btw. – Glenn Werner Jul 18 '17 at 10:49
  • When it says `SUCCESS: The process "node.exe" with PID XXX has been terminated.` 5 times... – Elijah Mock Sep 19 '20 at 21:10
  • still getting same error - Socket Error: SocketException: OS Error: Connection timed out, errno = 110, address = 10.1xx.3x.1xx, port = 4xxxx. only on real devices getting this error on emulator working fine. – s.j Apr 12 '21 at 11:02
  • 1
    Still working in 2021. – Junky May 20 '21 at 15:39
172

process.on('exit', ..) isn't called if the process crashes or is killed. It is only called when the event loop ends, and since server.close() sort of ends the event loop (it still has to wait for currently running stacks here and there) it makes no sense to put that inside the exit event...

On crash, do process.on('uncaughtException', ..) and on kill do process.on('SIGTERM', ..)

That being said, SIGTERM (default kill signal) lets the app clean up, while SIGKILL (immediate termination) won't let the app do anything.

Tor Valamo
  • 30,859
  • 10
  • 70
  • 79
106

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
Ayan
  • 5,592
  • 3
  • 30
  • 37
  • 2
    I get no output if I use lost. Does this mean there is no process on that part? – x89 Jan 23 '20 at 09:04
  • Don't use `kill -9` unless you know what you are doing. Basically, try `kill PID` and `kill -2 PID` and `kill -15 PID` and wait a while, and if the process is still around, *then* bring out the big gun. Killing a process unconditionally doesn't let it do any cleanup, which could lead to file handle leaks and race conditions etc. Or cause the "address already in use" for that matter. – tripleee Jan 15 '21 at 05:43
75

I found this solution, try it Give permission use sudo

  sudo pkill node
Balaji Rajendran
  • 307
  • 5
  • 17
alicanozkara
  • 3,945
  • 1
  • 21
  • 22
37

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.

Dharman
  • 21,838
  • 18
  • 57
  • 107
Genius
  • 1,116
  • 12
  • 21
36

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
Cody Gray
  • 222,280
  • 47
  • 466
  • 543
Shripad Krishna
  • 10,045
  • 4
  • 50
  • 63
29

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

kill $(lsof -t -i:3000)
floribon
  • 18,567
  • 4
  • 50
  • 66
27

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

enter image description here

Shahdat
  • 4,824
  • 3
  • 34
  • 36
20

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).

Kit
  • 3,038
  • 1
  • 24
  • 23
  • the same issue here... weird that it was properly working in debugging while getting this error for running `npm start` – Asqan Mar 11 '19 at 18:15
17
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.
Daniyal
  • 433
  • 4
  • 8
  • Don't use `kill -9` unless you know what you are doing. Basically, try `kill PID` and `kill -2 PID` and `kill -15 PID` and wait a while, and if the process is still around, *then* bring out the big gun. Killing a process unconditionally doesn't let it do any cleanup, which could lead to file handle leaks and race conditions etc. Or cause the "address already in use" for that matter. – tripleee Jan 15 '21 at 05:42
16

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.

Acumenus
  • 41,481
  • 14
  • 116
  • 107
Dat TT
  • 2,106
  • 13
  • 15
15

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

jmrueda
  • 974
  • 11
  • 24
14

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

raison
  • 1,563
  • 1
  • 21
  • 38
13

PowerShell users:

Taskkill /IM node.exe /F

var foobar
  • 139
  • 1
  • 3
13
bash$ sudo netstat -ltnp | grep -w ':3000'
 - tcp6    0      0 :::4000      :::*        LISTEN      31157/node      

bash$ kill 31157
tripleee
  • 139,311
  • 24
  • 207
  • 268
Yanov
  • 480
  • 5
  • 8
12

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.

Adam Gerard
  • 498
  • 1
  • 6
  • 21
12

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.

SendETHToThisAddress
  • 1,477
  • 2
  • 15
  • 29
9

Task Manager (ctrl+alt+del) ->

Processes tab ->

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

idanuda
  • 494
  • 5
  • 19
  • why kill the entire node process if what am looking is to kill a node port process . – Jimmy Obonyo Abor Oct 01 '16 at 22:12
  • That doesn't make sense, there is no separate process for holding the port. If you have several `node` processes accessing different ports, or none at all, you have to single out the one which is holding on to the port; but it will still be just a `node` process. – tripleee Jan 15 '21 at 05:46
9

In Linux try

pkill nodejs 
//or 
pkill node
MD SHAYON
  • 2,833
  • 21
  • 18
7

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

Najathi
  • 747
  • 11
  • 15
7

Windows by Cmd

1/2. search => write cmd => open node.js command prompt

enter image description here


2/2. Run windows command: taskkill

Ends one or more tasks or processes.

taskkill /f /im node.exe

/f - force ended

/im - Specifies the image name of the process to be terminated.

node.exe - executable file

enter image description here

Windows - Mannualy by Task Manager

This command is the same as going to Task Manager under the details tab & select node tasks (Tidy in my opinion).

enter image description here

And end task

enter image description here

Visual studio

Sometimes there is more than one terminal/task (client/server and so on). Select and close by ctrl + c.

enter image description here

Ezra Siton
  • 3,516
  • 2
  • 10
  • 16
5

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.

  • 1
    Ctrl + C is the correct. I used the same port number as before and it worked as before. – vipulnj Jun 10 '17 at 02:57
  • 2
    Ctrl-Z doesn't stop the process. It puts it in the background so you can run other commands. This is a Unix shell thing. To continue the process use `fg` in that same console. You can then see what's happening in that server after having typed various commands in the command line. – Alexis Wilke Jan 18 '19 at 02:24
4

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

touchStone
  • 318
  • 1
  • 16
  • pm2 is a better choice. more robust, more options. and doesn't have the problem when running as root that forever has. – Lucas Jul 24 '14 at 10:45
  • @Lucas What is this root problem in forever that you speak of? I am unfortunately forced to use forever instead of pm2 on a product at work (due to some licensing crap), and this worries me a lot! – GPX Feb 04 '15 at 19:13
4

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.

Arun Killu
  • 11,533
  • 5
  • 29
  • 57
4

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

rofrol
  • 12,038
  • 7
  • 62
  • 63
  • 1
    Don't use `kill -9` unless you know what you are doing. Basically, try `kill PID` and `kill -2 PID` and `kill -15 PID` and wait a while, and if the process is still around, *then* bring out the big gun. Killing a process unconditionally doesn't let it do any cleanup, which could lead to file handle leaks and race conditions etc. Or cause the "address already in use" for that matter. – tripleee Jan 15 '21 at 05:44
3

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.

vapurrmaid
  • 2,141
  • 2
  • 11
  • 27
  • 1
    i'm getting the same issue with essentially the same setup. in my case i noticed that running an express app through npm spawns two processes, but only one is closed when using ctrl+c. when starting the app with just node, only one process is open and it closes correctly. – worc Dec 06 '17 at 22:55
  • 1
    this [git for windows issue](https://github.com/git-for-windows/git/issues/1248) is kind of shedding light on the whole thing. it seems like there might be a bug somewhere between mintty and a cygwin dependency. – worc Dec 07 '17 at 01:21
3

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

Raghav
  • 6,381
  • 4
  • 60
  • 85
2

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.

Maksim Vi.
  • 8,589
  • 12
  • 54
  • 83
user1354017
  • 431
  • 6
  • 11
2

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();
}
Andreas Bigger
  • 3,471
  • 1
  • 9
  • 16
1

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;
Vish
  • 790
  • 3
  • 19
1

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);
Ruchir
  • 908
  • 6
  • 15
1

Simple exit from the server and change the server port 3000 to 31000 and its working fine.

Xeeshan Sami
  • 83
  • 1
  • 9
0

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

shadrack Mwangi
  • 166
  • 1
  • 11
0

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.

ankhzet
  • 2,358
  • 1
  • 19
  • 29
0

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

Irfan Raza
  • 2,654
  • 1
  • 19
  • 28
0

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

Anfuca
  • 1,231
  • 1
  • 14
  • 25
0

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)

Bernardo Dal Corno
  • 1,251
  • 1
  • 16
  • 21
0

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.

Yogesh Nogia
  • 946
  • 8
  • 11
0

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.

0

I had another problem. I had declared the port twice. Don't do that mistake as well.

app.listen(port, function () {
  console.log('Example app listening on port')
})

app.listen(3000, function() {
  console.log("Server started on port 3000");
});

Instead do this:

const port = 3000;

app.listen(port, function () {
  console.log('Example app listening on port')
})
Mehul
  • 34
  • 4
-1

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

-2

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

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

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.

fcdt
  • 2,151
  • 5
  • 9
  • 24
agnes_st
  • 27
  • 2