node.js - ExpressJS - throw er Unhandled error event

ID : 20061

viewed : 7

Tags : node.jsexpressnpmnode.js

Top 5 Answer for node.js - ExpressJS - throw er Unhandled error event

vote vote

97

You had run another server use the same port like 8080.

Maybe you had run node app in other shell, Please close it and run again.

You can check PORT no. is available or not using

netstat -tulnp | grep <port no> 

Alternatively, you can use lsof:

lsof -i :<port no> 
vote vote

86

We do get similar error when we sometimes run our express app. We have to follow the same in that case. We need to check if its running in any terminal. If you want to find and kill process, follow these steps:

  • ps aux | grep node
  • Find the process ID (second from the left):
  • kill -9 PRCOCESS_ID

OR

Use a single command to close all the running node processes.

ps aux | awk '/node/{print $2}' | xargs kill -9 
vote vote

77

An instance is probably still running. This will fix it.

killall node 

Update: This command will only work on Linux/Ubuntu & Mac.

vote vote

64

If you're on Linux, this problem can also occur if Nodejs is not running as root.

Change from this:

nodejs /path/to/script.js 

To this:

sudo nodejs /path/to/script.js 

Just happened to me and none of the other suggestions here fixed it. Luckily I remembered the script was working the other day when running as root. Hope this helps someone!

Disclaimer: This probably isn't the best solution for a production environment. Starting your service as root may introduce some security holes to your server/application. In my case, this was a solution for a local service, but I'd encourage others to spend some more time trying to isolate the cause.

vote vote

52

This is because the port you are using to run the script is already in use. You have to stop all other nodes which are using that post. for that, you can check all node by

ps -e 

OR for node process only use ps -ef | grep node This will give you the list of all node process with id

to Kill all node process

sudo killall -9 node 

Or for the specific id sudo kill -9 id

Top 3 video Explaining node.js - ExpressJS - throw er Unhandled error event

Related QUESTION?