DEV Community

Cover image for Fixing nodemon 'Error: listen EADDRINUSE: address in use'
Matt Heindel
Matt Heindel

Posted on • Updated on

Fixing nodemon 'Error: listen EADDRINUSE: address in use'

Has this ever happened to you?

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

$ 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...
Enter fullscreen mode Exit fullscreen mode

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.

kill -9 $(lsof -t -i:3000)
Enter fullscreen mode Exit fullscreen mode

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.

killport() { kill -9 $(lsof -t -i:"$@"); } # kill process on specified port number
Enter fullscreen mode Exit fullscreen mode

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.

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

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

killport 3000
Enter fullscreen mode Exit fullscreen mode

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.

killport() { kill -9 $(lsof -t -i:"$@"); } # kill process on specified port number
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
zbretz profile image
Zach

The wait is over! Killer post.

We ran into this yesterday and had to hunt down the process that was running. We'll use this next time. It'll be a time saver for sure.

Collapse
 
mattheindel profile image
Matt Heindel

Glad I could help spare you some headache!

Collapse
 
nosliwsirhc profile image
Chris Wilson

If you're using ts-node with npx, I was having the same issue. I installed nodemon and ts-node as dev dependencies then removed npx and don't have the problem anymore.