<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Matt Heindel</title>
    <description>The latest articles on DEV Community by Matt Heindel (@mattheindel).</description>
    <link>https://dev.to/mattheindel</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F649028%2F0a8d2faf-6f6d-493c-972a-7eb2f89d5b1c.jpeg</url>
      <title>DEV Community: Matt Heindel</title>
      <link>https://dev.to/mattheindel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mattheindel"/>
    <language>en</language>
    <item>
      <title>Fixing nodemon 'Error: listen EADDRINUSE: address in use'</title>
      <dc:creator>Matt Heindel</dc:creator>
      <pubDate>Thu, 08 Jul 2021 05:44:23 +0000</pubDate>
      <link>https://dev.to/mattheindel/fixing-nodemon-error-listen-eaddrinuse-address-in-use-3ic2</link>
      <guid>https://dev.to/mattheindel/fixing-nodemon-error-listen-eaddrinuse-address-in-use-3ic2</guid>
      <description>&lt;h2&gt;
  
  
  Has this ever happened to you?
&lt;/h2&gt;

&lt;p&gt;You go to start up your server with &lt;code&gt;npm start&lt;/code&gt; and you get the below error message&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm start

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; cruddy-todos@1.0.0 start /home/mc_heindel/HackReactor/hr-rfp54-cruddy-todo
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; npx nodemon ./server.js

&lt;span class="o"&gt;[&lt;/span&gt;nodemon] 2.0.6
&lt;span class="o"&gt;[&lt;/span&gt;nodemon] to restart at any &lt;span class="nb"&gt;time&lt;/span&gt;, enter &lt;span class="sb"&gt;`&lt;/span&gt;rs&lt;span class="sb"&gt;`&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;nodemon] watching path&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt;: &lt;span class="k"&gt;*&lt;/span&gt;.&lt;span class="k"&gt;*&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;nodemon] watching extensions: js,mjs,json
&lt;span class="o"&gt;[&lt;/span&gt;nodemon] starting &lt;span class="sb"&gt;`&lt;/span&gt;node ./server.js&lt;span class="sb"&gt;`&lt;/span&gt;
events.js:292
      throw er&lt;span class="p"&gt;;&lt;/span&gt; // Unhandled &lt;span class="s1"&gt;'error'&lt;/span&gt; event
      ^

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Luckily there is a solution!
&lt;/h2&gt;

&lt;p&gt;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 &lt;code&gt;3000&lt;/code&gt; we could simply paste and execute the below code in our terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;kill&lt;/span&gt; &lt;span class="nt"&gt;-9&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;lsof &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt;:3000&lt;span class="si"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will kill the process running on port 3000 and you should be good to start your server with &lt;code&gt;npm start&lt;/code&gt; like usual.&lt;/p&gt;

&lt;h2&gt;
  
  
  Save this command for future use
&lt;/h2&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;killport&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;kill&lt;/span&gt; &lt;span class="nt"&gt;-9&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;lsof &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt;:&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="c"&gt;# kill process on specified port number&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The quick and easy way to save
&lt;/h3&gt;

&lt;p&gt;Simply execute the below command and the &lt;code&gt;killport&lt;/code&gt; 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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'killport() { kill -9 $(lsof -t -i:"$@"); } # kill process on specified port number'&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below is an example of how the newly defined &lt;code&gt;killport&lt;/code&gt; function can be used to kill a process on port 3000.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;killport 3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The slightly more advanced way to save
&lt;/h3&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;killport&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;kill&lt;/span&gt; &lt;span class="nt"&gt;-9&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;lsof &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt;:&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="c"&gt;# kill process on specified port number&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
