node.js error message events.js:183 throw er; // Unhandled 'error' event

yu zhou picture yu zhou · Feb 13, 2018 · Viewed 25.2k times · Source

Playing around Microsoft's botframework, when I try to run the app.js file, which is the main file of the bot, the first time is fine, after I close the bot emulator, and all programs, and run the app.js again, this error message pops out

events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::3978
    at Object._errnoException (util.js:1022:11)
    at _exceptionWithHostPort (util.js:1044:20)
    at Server.setupListenHandle [as _listen2] (net.js:1351:14)
    at listenInCluster (net.js:1392:12)
    at Server.listen (net.js:1476:7)
    at Server.listen (C:\Users\yu\Documents\GitHub\BotBuilder-
Samples\Node\cards-AdaptiveCards\node_modules\restify\lib\server.js:404:32)
    at Object.<anonymous> (C:\Users\yu\Documents\GitHub\BotBuilder-
Samples\Node\cards-AdaptiveCards\app.js:10:8)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)

I found some solution to this, It looks like the port 3978 was taken when I run the js file for the second time, and I have to use cmd to find which task is using that port, and then kill the task.

C:\>netstat -aon|findstr 3978

so this one is taking the port, although I already closed all programs

TCP    0.0.0.0:3978           0.0.0.0:0              LISTENING       13140
TCP    [::]:3978              [::]:0                 LISTENING       13140

actually it is node.exe itself, it still running at background

C:\Users\yu>tasklist|findstr 13140
node.exe                     13140 Console                    1     42,064 K

and I need to kill it

C:\Users\yu>taskkill /f /t /im node.exe

Is there a way to not always repeat doing this when I want to run the file more than one time, it just starts to go wired today, and I mean for all different app.js files, include offical sample code. For the past one month, everything was fine, the IDE I am using is sublime text 3, node.js version is 8.9.4 lts

Answer

Natesh bhat picture Natesh bhat · Sep 21, 2018

This is a very common issue and can be easily fixed by either of the below methods.

  1. Try to close the process that is using your port.

    netstat -tulnp | grep <port_number>

  2. Installing the below pakcage fixed it for me forever.

    npm install [email protected] --save-dev --save-exact

  3. Run this command in your terminal :

    echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

    For Arch Linux add this line to /etc/sysctl.d/99-sysctl.conf:

    fs.inotify.max_user_watches=524288

    Then execute:

    sysctl --system

    This will also persist across reboots.

    https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers#the-technical-details

Either one of the solutions will work . If not then restart your system and check again.