ChatGPT解决这个技术问题 Extra ChatGPT

Node.js EACCES error when listening on most ports

I'm testing out an app (hopefully to run on heroku, but am having issues locally as well). It's giving me an EACCES error when it runs http.Server.listen() - but it only occurs on some ports.

So, locally I'm running:

joe@joebuntu:~$ node
> var h = require('http').createServer();
> h.listen(900);
Error: EACCES, Permission denied
    at Server._doListen (net.js:1062:5)
    at net.js:1033:14
    at Object.lookup (dns.js:132:45)
    at Server.listen (net.js:1027:20)
    at [object Context]:1:3
    at Interface.<anonymous> (repl.js:150:22)
    at Interface.emit (events.js:42:17)
    at Interface._onLine (readline.js:132:10)
    at Interface._line (readline.js:387:8)
    at Interface._ttyWrite (readline.js:564:14)

I don't have anything running on port 900 (or any of the other 20 ports I've tried), so this should work. The weird part is that it does work on some ports. For instance, port 3000 works perfectly.

What would cause this?

Update 1:

I figured out that on my local computer, the EACCES error is coming because I have to run node as root in order to bind to those certain ports. I don't know why this happens, but using sudo fixes it. However, this doesn't explain how I would fix it on Heroku. There is no way to run as root on Heroku, so how can I listen on port 80?

Ports less 1024 traditionally require elevated permissions. On Heroku you dont listen to port 80, you listen to the port they tell you to via environment variables, and let their routing layer handle the port 80 binding on the edge.
Your update 1 helped me. 'sudo node myporgram.js' made it run.
In case this helps anyone: double and triple check you are listening on a port number. Due to a bug I was listening on some string and then you get the confusing EACCES errpr.
@Marc you're right. The port needs to be a number, especially when using https server. I've posted an answer for this here: stackoverflow.com/a/70950946/10030693

B
Ben Taber

Running on your workstation

As a general rule, processes running without root privileges cannot bind to ports below 1024.

So try a higher port, or run with elevated privileges via sudo. You can downgrade privileges after you have bound to the low port using process.setgid and process.setuid.

Running on heroku

When running your apps on heroku you have to use the port as specified in the PORT environment variable.

See http://devcenter.heroku.com/articles/node-js

const server = require('http').createServer();
const port = process.env.PORT || 3000;

server.listen(port, () => console.log(`Listening on ${port}`));

Does this mean I have to use the port provided by Heroku, and then they will do some magic behind the scenes to transfer that to port 80? What if I want to run something not on port 80?
Yes. You can only listen to the port we tell you on $PORT We take care of routing 80 or 443 to your port. The actual port changes all of the time as we move your dyno around. At this point in time we only support publicly routing from 80 and 443.
@Will, any chance of that restriction lifting? Specifically, being able to listen on ports other than 80 or 443? That's a pretty restrictive set, all things considered.
Why do you want your app running on a different port than http and https?
@Will I would like to host a simple game server on Heroku. Unity needs to transfer crossdomain.xml via 843 port.
u
user1303768

Non-privileged user (not root) can't open a listening socket on ports below 1024.


Upvoted - this is a good general rule, but there are exceptions to this, eg, 'capabilities' on Linux.
You just saved me hours of debug. I didn't know about that.
I dont like this though, i dont want to have to sudo when running an express server using node (gulp actually). So doesnt make sense
this piece of wisdom
@blamb Then use MeetMehta's solution ;^)
N
Nigrimmist

#Windows

Another one reason - maybe your port has been excluded by some reasons.

So, try open CMD (command line) under admin rights and run :

net stop winnat net start winnat

https://i.stack.imgur.com/5V5ob.png

In my case it was enough.

Solution found here : https://medium.com/@Bartleby/ports-are-not-available-listen-tcp-0-0-0-0-3000-165892441b9d


It's the only solution that worked for me.
This solved my problem, thanks. However, can anybody explain what has caused this problem suddenly?
This work for me
Worked for me as well!
D
DJCrashdummy

Check this reference link:

Give Safe User Permission To Use Port 80 Remember, we do NOT want to run your applications as the root user, but there is a hitch: your safe user does not have permission to use the default HTTP port (80). You goal is to be able to publish a website that visitors can use by navigating to an easy to use URL like http://ip:port/ Unfortunately, unless you sign on as root, you’ll normally have to use a URL like http://ip:port - where port number > 1024. A lot of people get stuck here, but the solution is easy. There a few options but this is the one I like. Type the following commands: sudo apt-get install libcap2-bin sudo setcap cap_net_bind_service=+ep `readlink -f \`which node\`` Now, when you tell a Node application that you want it to run on port 80, it will not complain.


This is definitely the best solution.
@MarkLagendijk: Thanks Mark. I also asked the same question mistakenly and posted detailed answer over here stackoverflow.com/questions/23281895/…. Feel free to edit it as well.
why isn't this the answer 👍
@KhaledMohamedP: Glad it helped :)
Who says this still not works with pm2 module. Kill your pm2 using pm2 kill and recreate it.
S
Sandeep K

On Windows System, restarting the service "Host Network Service", resolved the issue.


This needs more upvotes! Thank you - sometimes I ran into this problem and always had to restart the whole machine.
This solved the issue for me! Can anyone help me understand why and how?
finally an answer better than restarting!
R
Ridwan Malik

If you are using windows. You should try restarting Windows NAT Driver service.

Open Command Prompt as Administrator and run

net stop winnat

then

net start winnat

That's it.

It's happening because I installed Nord VPN. and it was auto staring with windows.


e
eleven

Another approach is to make port redirection:

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 900 -j REDIRECT --to-port 3000

And run your server on >1024 port:

require('http').createServer().listen(3000);

ps the same could be done for https(443) port by the way.


Thank you, thank you! This saved me one hour of debugging for safe ports, another hour for SSL redirection, and also allowed me to restrict the visibility of a development server on AWS Lightstail (which does not allow fine-tuning of requests by IP address).
D
Dilip Muthukurussimana

OMG!! In my case I was doing ....listen(ip, port) instead of ...listen(port, ip) and that was throwing up the error msg: Error: listen EACCES localhost

I was using port numbers >= 3000 and even tried with admin access. Nothing worked out. Then with a closer relook, I noticed the issue. Changed it to ...listen(port, ip) and everything started working fine!!

Just calling this out in case if its useful to someone else...


Thank you! I had the same problem. I'm used to all other APIs using hostname, port.
I had this problem when I trying run Wekan docker image. I solved using this tip. Thank you.
@dilip-muthukurussimana Did you intend to have ....listen(ip, port) in your answer (with four .)? It took me a minute to realize you were talking about the order of the arguments because of that.
Yes, I meant only the order of arguments. Pls don't put .... (four dots) there, which I thought was obvious.
The reverse was the case for me. I am using Nodejs v14.17.5 I had been using ....listen(port, IP) and it was throwing errors listen EACCES: permission denied 127.0.0.1. I saw your answer, which made me pay closer attention to the error, I changed it to ....listen(ip, port) and it worked.
B
Bo Persson

It means node is not able to listen on defined port. Change it to something like 1234 or 2000 or 3000 and restart your server.


F
Fusseldieb

I had a similar problem that it was denying to run on port 8080, but also any other.

Turns out, it was because the env.local file it read contained comments after the variable names like:

PORT=8080 # The port the server runs at

And it interpreted it like that, trying to use port "8080 # The port the server runs at", which is obviously an invalid port (-1). Removing the comments entirely solved it.

Using Windows 10 and Git Bash by the way.

I know it's not exactly the problem described here, but it might help someone out there. I landed on this question searching for the problem for my answer, so... maybe?


Yep, I've experienced this. Comments after values in .env files can cause this.
l
luffy

Spoiler alert: This answer may seems little funny.

I have spent more than 10 minutes to find out the root cause for this error in my system. I used this : PORT=2000; in my .env file.

Hope you already find it out. I had used a semicolon after declaring PORT number :'( I removed the extra sign and it started working.

I know this may not be answer for this question but hope it helps others who have faced same problem.


Okay I did the same mistake and couldn't find the error and wasted a day . This answer helped me. Thanks
Thanks, bro. I did the same mistake :-)
m
mabounassif

I got this error on my mac because it ran the apache server by default using the same port as the one used by the node server which in my case was the port 80. All I had to do is stop it with sudo apachectl stop

Hope this helps someone.


b
bschlueter

I got this error on my mac too. I use npm run dev to run my Nodejs app in Windows and it works fine. But I got this error on my mac - error given was: Error: bind EACCES null:80.

One way to solve this is to run it with root access. You may use sudo npm run dev and will need you to put in your password.

It is generally preferable to serve your application on a non privileged port, such as 3000, which will work without root permissions.

reference: Node.js EACCES error when listening on http 80 port (permission denied)


F
Fahima Mokhtari

restart was not enough! The only way to solve the problem is by the following:

You have to kill the service which run at that port.

at cmd, run as admin, then type : netstat -aon | find /i "listening"

Then, you will get a list with the active service, search for the port that is running at 4200n and use the process id which is the last column to kill it by

: taskkill /F /PID 2652


P
PA.

Remember if you use sudo to bind to port 80 and are using the env variables PORT & NODE_ENV you must reexport those vars as you are now under root profile and not your user profile. So, to get this to work on my Mac i did the following:

sudo su
export NODE_ENV=production
export PORT=80
docpad run

J
Jasper Smith

this happens if the port you are trying to locally host on is portfowarded


D
DraughtGlobe

Try authbind:

http://manpages.ubuntu.com/manpages/hardy/man1/authbind.1.html

After installing, you can add a file with the name of the port number you want to use in the following folder: /etc/authbind/byport/

Give it 500 permissions using chmod and change the ownership to the user you want to run the program under.

After that, do "authbind node ..." as that user in your project.


P
Prathamesh More

My error is resolved using (On Windows)

app.set('PORT', 4000 || process.env.PORT);

app.listen(app.get('PORT'), <IP4 address> , () => {
    console.log("Server is running at " + app.get('PORT'));
});

Allow the NodeJS app to access the network in Windows Firewall.


A
Alok Ranjan

My error got resolved just by changing port number in server.js Specially in this line

const port = process.env.PORT || 8085;

I changed my port number to 8085 from 8080.

Hope it helps.


m
mrjamiebowman

For me this issue affected all hosts and all ports on Windows in PowerShell.

Disabling Network Interfaces fixed the issue.

I had WiFi and an Ethernet connection and disabling the Ethernet Interface fixed this issue.

Open "Network Connections" to view your interfaces. Right-click and select "Disable".


H
Hosayn Sayed

This means the port is used somewhere else. so, you need to try another one or stop using the old port.


C
CageE

After trying many different ways, re-installing IIS on my windows solved the problem.


Z
Zeeshan Ali Khan

The same issue happened to me. You need to check out your server.js file where you are setting your listening port. Change port number wisely in all places, and it will solve your issue hopefully.


A
Adrian Mole

For me, it was just an error in the .env file. I deleted the comma at the end of each line and it was solved.

Before:

HOST=127.0.0.1,

After:

HOST=127.0.0.1

O
Or Assayag

For me, the issue was exiting my node application before without closing the express running server.


k
khizar ijaz

Error: listen EACCES: permission denied 3000; i add "PORT = 3000;" while "PORT = 3000" . just semicolon";" give error

remove semicolon and project run successfully


Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
1
19UEC115 SOHAM SAKHARELIYA

I had a similar problem that it was denying to run on port 5000,

Turns out, it was because the env.local file contained comma(';') after variable names like:

PORT= 5000; And it interpreted it like that, trying to use port "5000;", which is obviously an invalid port (-1). Removing the ';' entirely solved it.

I know it's not exactly the problem described here, but it might help someone out there. I landed on this question searching for the problem for my answer, so... maybe?


Please target the answer to the question here. Look for the relevant parts. Answering a different question, even if similar, is not appropriate.
On the other hand, if this does answer the question, but only with info which is already in other answers (e.g. stackoverflow.com/a/71235768/7733418 ) this answer is not needed at all either.
M
Magdi T.

This worked perfectly fine for me, set your port at the bottom of the page with this code instead

     let port = process.env.PORT;
if (port == null || port == "") {
    port = 3000;
}

app.listen(port, function() {
    console.log('app started successfully')
});

S
Sayeed Mahdi Mousavi

Some times it is because of bad configuration the dot.env like: require("dotenv").config without () in your app middle ware or may be you write your port number with wrong syntax like instead of = you write : or add some other symbols in port number.


T
The Gentelmen 24

What works for me is that,

Solution 1: I find that my .env file is incorrect. You should not put ',' or ';' separator between the variables.`

Solution 2: If it's a react app be sure you're in the client directory and that you're not trying to run your react app in the server directory (not give you an error but address the issue.)