Fixing nodemon 'Error: listen EADDRINUSE: address in use'
Published: 2021-07-08 05:44:23

## Has this ever happened to you?

You go to start up your server with `npm start` and you get the below error message

```bash
$ npm start

> cruddy-todos@1.0.0 start /home/mc_heindel/HackReactor/hr-rfp54-cruddy-todo
> npx nodemon ./server.js

[nodemon] 2.0.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node ./server.js`
events.js:292
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (net.js:1318:16)
    at listenInCluster (net.js:1366:12)
    at Server.listen (net.js:1452:7)
    at Function.listen (/home/mc_heindel/HackReactor/hr-rfp54-cruddy-todo/node_modules/express/lib/application.js:618:24)
    at Object.<anonymous> (/home/mc_heindel/HackReactor/hr-rfp54-cruddy-todo/server.js:79:5)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
Emitted 'error' event on Server instance at:
    at emitErrorNT (net.js:1345:8)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  code: 'EADDRINUSE',
  errno: -98,
  syscall: 'listen',
  address: '::',
  port: 3000
}
[nodemon] app crashed - waiting for file changes before starting...
```

## Luckily there is a solution!

This error occurs when a process is already running on the port you're trying to use. All you need to do is kill that process. In this case, since the port we want to use is `3000` we could simply paste and execute the below code in our terminal. 

```bash
kill -9 $(lsof -t -i:3000)
```  

This will kill the process running on port 3000 and you should be good to start your server with `npm start` like usual.

## Save this command for future use

If this happens often, it's a great idea to add an alias to bash for reuse anytime. The below code is a function that accepts a port number to kill and when saved can be reused for any port number.

```bash
killport() { kill -9 $(lsof -t -i:"$@"); } # kill process on specified port number
```  

### The quick and easy way to save

Simply execute the below command and the `killport` function will be available every time you open bash. Just remember to restart your terminal for the saved function to be loaded after first adding it.

```bash
echo 'killport() { kill -9 $(lsof -t -i:"$@"); } # kill process on specified port number' >> ~/.bashrc
```  

Below is an example of how the newly defined `killport` function can be used to kill a process on port 3000. 

```bash
killport 3000
```  

### The slightly more advanced way to save

To save this function alongside your other bash aliases and configurations you just need to add the below code to `~/.bashrc` or `~/.bash_aliases` which can be accomplished using `vim ~/.bashrc` and pasting in the code snippet. 

```bash
killport() { kill -9 $(lsof -t -i:"$@"); } # kill process on specified port number
```