Node.js Unhandled 'error' event when using http.createServer().listen() on Ubuntu 12.04 - node.js

Salam (means Hello) :)
I've developed a node.js script on my windows seven machine and it's working fine. but when I run it on my Ubuntu 12.04, the following error shows up and halts my app:
throw er; // Unhandled 'error' event
^
Error: listen EACCES
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1020:19)
at listen (net.js:1061:10)
at Server.listen (net.js:1127:5)
at Object.start (/httpServer/httpServer.js:9:34)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
and the point that caused error is .listen(80) in this line:
http.createServer(onRequest).listen(80);
^
I've also tried some other port numbers (like 100, 300, 500,...) instead of 80 and the error was still the same.

On Ubuntu you can't listen on ports < 1024 without root privileges. Try running node under sudo.
sudo node app.js

You probably have apache running on port 80, so it's conflicting.
Use another port (NOT within 0-1023), or disable apache.
Cheers

You probably have something else running on port 80, so it's conflicting.
Read here to find out what is using port 80 and stop it
http://www.cyberciti.biz/faq/find-linux-what-running-on-port-80-command/
Usually it means another server like apache is enabled. so stop it.
sudo service apache2 stop
or You have npm start already running in another terminal
or skype is running. in which case go to settings and change it's port. logout n login
Go to Tools -> Options -> Advanced -> Connections and uncheck the box "use port 80 and 443 as alternative".src
or use another port
http-server -a localhost -p 8000

I suggest to install the latest node packets, maybe directly from Node.js server, maybe compiling it.
Try to set a port which is not reserved to any service, like 3700.
Could be heplful to see some other fragment of code, though.

This can also be caused if you have something else already listening on that port - you can try changing the port from the typical default 80 to something more like 10014 and see if that helps!

I was able to rectify the error by explicitely mentionining the "IP address" along with the port when listening to the server.

Related

Nodejs address already in use :::5000

I am trying to run a nodes app. I run node app.js. When I do, I get this error:
node:events:346
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::5000
at Server.setupListenHandle [as _listen2] (node:net:1311:16)
at listenInCluster (node:net:1359:12)
at Server.listen (node:net:1446:7)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1338:8)
at processTicksAndRejections (node:internal/process/task_queues:81:21) {
code: 'EADDRINUSE',
errno: -48,
syscall: 'listen',
address: '::',
port: 5000
}
I spent hours looking at possible solutions online but none have worked so far. Same codebase works great on a different machine. This error happens on Mac M1 chip.
Does anybody have any idea how to fix this?
I tried finding processes running on 5000, I tried to kill them etc... nothing worked so far. I am running node 15.14.0.
I am on a M1 Chip, running macOS Monterey
How can I fix this error?
You should disable AirPlay on your mac. Because macOS Monterey started to listen to port 5000 and 8000.
System Preferences > Sharing > untick AirPlay Receiver
For macOS Ventura:
System Settings > General > disable Airplay Receiver
Lee Jeonghyun was correct. The reason why kill -9 PID would never work is because of this:
https://developer.apple.com/forums/thread/682332
Control Center on Monterey is listening on 5000 as well.
I changed the port number in my nodejs app and the app started working again.
List all process on mac:
sudo lsof -PiTCP -sTCP:LISTEN
You will see a list of process and its ports. Choose the process PID on 5000 port or any other port in the error: EADDRINUSE: address already in use...
Finally kill it if it is not an important app for you
kill -9 1234
This also happened to me after upgrading to macOS Monterey from Big Sur.
Every port that I passed to my Node app appeared to be already in use.
You need to disable AirPlay on your mac.
System Preferences > Sharing > untick AirPlay Receiver
Screenshot
Did some investigation and found that macOS Monterey is using 5000 port for Airplay-Receiver. If you really want to use the port, then disable that feature in "System Preferences->Sharing".
Below medium post helped me:
Port 5000 already in use — MacOS Monterey issue

nodemon is crashing after several changes done in project files?

When I update the project Files I get this issue given Below:
events.js:183
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::4000
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 Function.listen (E:\nodejs-mysql-authentication- master\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (E:\nodejs-mysql-authentication-master\server.js:26:5)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
[nodemon] app crashed - waiting for file changes before starting...
I already try all this point to short out this issue given below
1.I manually stops the all node services than again I try npm start It's not worked if i change port 4001 it's work after, some time again same issue port 4001.
2.I Increase the nodemon file watcher size but still this issue not get solved.
But When I restart My PC again then nodemon is working on same port.
So How I can solve this issue, If I don't want to change my port and can't restart my PC.
Adding --signal SIGTERM to nodemon command line fixed for me. You can have more details at nodemon project # github.com
The issue occurs due to a thread still running when nodemon restarts the app, and this thread is already using the port you want to use. You can confirm this with these steps:
Make nodemon crash. You should see something like the error posted by OP
hit CTRL+c when it happens to stop nodemon
enter netstat -napt | grep [YOUR_PORT], replace [YOUR_PORT] with the port number you are using. In the OP's case it's 4000
you should get the process listening on that port. The command will return something like this:
ff#darkpc:~/dev/dp/graphql/teste1> netstat -napt | grep 4000
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp6 0 0 :::4000 :::* LISTEN 31837/node8
if you try to run nodemon again it will crash!
kill the process using your port with kill 31837 in this case 31387 is the PID of node8 as you can see above
run nodemon again and it will work.
Adding the --signal SIGTERM to nodemon command line will kill the main process and all it's threads and should fix the problem.

port 100 not running in node.js in localhost

I have created a http server in node.js with the following code, and trying to run it on port 100:
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end("Howdy");
}).listen(100);
console.log("server running on port 100");
With this, the server does not start and I am getting the following error message on the linux console:
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EACCES
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1020:19)
at listen (net.js:1061:10)
at Server.listen (net.js:1135:5)
at Object.<anonymous> (/home/badhai/Desktop/mainn.js:6:4)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
But if I lift a sails.js app on port 100, it runs successfully on port 100. However, the above code runs successfully on port 8081. I want to know if I need to make any changes in the server creation method or elsewhere so that it can be made to run successfully on port 100?
The EACCES part of the error message is the key here - it means you don't have access to that port. Ports < 1024 are system reserved. It's better to use ports in the range 1024-65535
Most modern operating systems limit binding to reserved ports (less than 1024) to processes running as root (or equivalent).
If you absolutely must bind to port 100, googling around will give you a bunch of ways to do it:
https://gist.github.com/firstdoit/6389682
It's better to use ports > 1024. I'm using ports starting from 3000. But if you really want to start it on port 100 and you understand what you're doing then install setcap and just allow to bind ports <1024.
> sudo apt-get install setcap
> sudo setcap 'cap_net_bind_service=+ep' /usr/local/bin/node
NodeJS can be installed in other directory also, so better to check where it is and call above setcap command with your path.
> which node
/usr/local/bin/node

npm start giving error

I have installed KrakenJs on Windows 7. I have created project by using following command :
Yo Kraken
When I tried to run the project, it is giving following error (even after restarting system) :
E:\nodejs\test\kraken\Dust-01>npm start
> dust-01#0.1.0 start E:\nodejs\test\kraken\Dust-01
> node index.js
Error: listen EACCES
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1020:19)
at listen (net.js:1061:10)
at Server.listen (net.js:1127:5)
at bind (E:\nodejs\test\kraken\Dust-01\node_modules\kraken-js\index.js:139:33)
at _fulfilled (E:\nodejs\test\kraken\Dust-01\node_modules\kraken-js\node_modules\q\q.js:798:54)
at self.promiseDispatch.done (E:\nodejs\test\kraken\Dust-01\node_modules\kraken-js\node_modules\
q\q.js:827:30)
at Promise.promise.promiseDispatch (E:\nodejs\test\kraken\Dust-01\node_modules\kraken-js\node_mo
dules\q\q.js:760:13)
at E:\nodejs\test\kraken\Dust-01\node_modules\kraken-js\node_modules\q\q.js:574:44
at flush (E:\nodejs\test\kraken\Dust-01\node_modules\kraken-js\node_modules\q\q.js:108:17)
E:\nodejs\test\kraken\Dust-01>
Can some one guide me what I am doing wrong and how it can be rectified
Look into next two possibilities:
Try opening your command prompt 'As Administrator', that shall solve the issue if you are lacking permissions to open port.
Temporarily change the port used in Kraken to something irregular and long, example: 32482
Then move on from this, if 1st - then you have solved an issue, if 2nd - you are very likely trying to open a port, which is already used by other application.

Strange error message in Node.js

I've been getting a lot of strange error message lately, and now I can't even get app.js started. What could the following mean, and does anyone know how to solve it?
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: Cannot read property 'port' of null
at Object.<anonymous> (/Users/henrikpetersson81/node/last/test4/app.js:15:64)
at Module._compile (module.js:441:26)
at Object..js (module.js:459:10)
at Module.load (module.js:348:31)
at Function._load (module.js:308:12)
at Array.0 (module.js:479:10)
at EventEmitter._tickCallback (node.js:192:40)
The port is propably already used. It was with me the case.
Saw a similar question recently. You could be experiencing a similar issue. Perhaps one of the libraries you are using (or your own code) is trying to access app.address().port before the app.listen has completed and the corresponding callback has been invoked.
I found an article explaining the same error message, and it's a problem with the port. I changed the port from 3000 to 5959 and now it works. Strange though that the port suddenly stopped working.
I had the same issue. It turns out while using nodemon( or simple for that matter) when u kill the node server it might not have been killed, so a server is still listening to 3000 port. To overcome this simple close the terminal and restart the server.(On ubuntu machine , using node and Nodemon)
I had a similar issue, where port:3000 is solely used by node.js script.
Killing the PID worked for me for port:3000.
lsof -i :3000
kill -9 <PID>
the port that your localhost is listening to, is being used by some other application/service. try changing the port at the following location to a different port number like 8888 or 3000 and you should be good to go.
/Users/henrikpetersson81/node/last/test4/app.js

Resources