Running node app forever with sudo leads to errors [duplicate] - javascript

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?

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}`));

#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
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

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

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.

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

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.

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.

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.

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...

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?

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.

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

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.

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

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)

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

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.

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.

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.

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".

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

I tried every answer given above, but nothing works out, then I figured out that I forget to add const before declaring the variable in the .env file.
Before:
PORT = 5000;
HOST = "127.0.0.1";
After:
const PORT = 5000;
const HOST = "127.0.0.1";

So the possible reason for this would be related to typo in environment variables names or else not installed dotenv package .
So if there is any other error apart from typo and dotenv npm package ,then you must try these solutions which are given below:
First solution
for Windows only
Open cmd as run as administrator and then write two commands
net stop winnat
net start winnat
hope this may solve the problem ...
Second solution
for windows only
make sure to see that in your environment variable that there is no semicolon(;) at the end of the variable and there is no colon (:) after the variable name.
for example I was working on my project in which my env variables were not working so the structure for my .env file was like PORT:5000; CONNECTION_URL:MongoDbString.<password>/<dbname>; so it was giving me this error
Error: listen EACCES: permission denied 5000;
at Server.setupListenHandle [as _listen2] (node:net:1313:21)
at listenInCluster (node:net:1378:12)
at Server.listen (node:net:1476:5)
at Function.listen (E:\MERN REACT\mern_memories\server\node_modules\express\lib\application.js:618:24)
at file:///E:/MERN%20REACT/mern_memories/server/index.js:29:9
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1357:8)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'EACCES',
errno: -4092,
syscall: 'listen',
address: '5000;',
port: -1
}
[nodemon] app crashed - waiting for file changes before starting...
So i did some changes in my env file this time i removed the colon(:) and replaced it with equal(=) and removed semi colon at the end so my .env file was looking like this
PORT = 5000
CONNECTION_URL = MongoDbString.<password>/<dbname>
After changing these thing my server was running on the port 5000 without any warning and issues
Hope this may works...
#code #developers #mernstack #nodejs #react #windows #hostservicenetwork #http #permission-denied #EACCES:-4092

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

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.

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

Error: listen EACCES: permission denied 3000;
i add "PORT = 3000;" while "PORT = 3000" .
just semicolon";" give error
remove semicolon and project run successfully

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?

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')
});

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.

Related

live-server Error: listen EACCES 0.0.0.0:8080. even changing the ports not working? [duplicate]

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?
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}`));
#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
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
Non-privileged user (not root) can't open a listening socket on ports below 1024.
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.
On Windows System, restarting the service "Host Network Service", resolved the issue.
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.
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.
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.
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...
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?
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.
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
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.
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
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)
this happens if the port you are trying to locally host on is portfowarded
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.
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.
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.
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".
This means the port is used somewhere else. so, you need to try another one or stop using the old port.
I tried every answer given above, but nothing works out, then I figured out that I forget to add const before declaring the variable in the .env file.
Before:
PORT = 5000;
HOST = "127.0.0.1";
After:
const PORT = 5000;
const HOST = "127.0.0.1";
So the possible reason for this would be related to typo in environment variables names or else not installed dotenv package .
So if there is any other error apart from typo and dotenv npm package ,then you must try these solutions which are given below:
First solution
for Windows only
Open cmd as run as administrator and then write two commands
net stop winnat
net start winnat
hope this may solve the problem ...
Second solution
for windows only
make sure to see that in your environment variable that there is no semicolon(;) at the end of the variable and there is no colon (:) after the variable name.
for example I was working on my project in which my env variables were not working so the structure for my .env file was like PORT:5000; CONNECTION_URL:MongoDbString.<password>/<dbname>; so it was giving me this error
Error: listen EACCES: permission denied 5000;
at Server.setupListenHandle [as _listen2] (node:net:1313:21)
at listenInCluster (node:net:1378:12)
at Server.listen (node:net:1476:5)
at Function.listen (E:\MERN REACT\mern_memories\server\node_modules\express\lib\application.js:618:24)
at file:///E:/MERN%20REACT/mern_memories/server/index.js:29:9
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1357:8)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'EACCES',
errno: -4092,
syscall: 'listen',
address: '5000;',
port: -1
}
[nodemon] app crashed - waiting for file changes before starting...
So i did some changes in my env file this time i removed the colon(:) and replaced it with equal(=) and removed semi colon at the end so my .env file was looking like this
PORT = 5000
CONNECTION_URL = MongoDbString.<password>/<dbname>
After changing these thing my server was running on the port 5000 without any warning and issues
Hope this may works...
#code #developers #mernstack #nodejs #react #windows #hostservicenetwork #http #permission-denied #EACCES:-4092
After trying many different ways, re-installing IIS on my windows solved the problem.
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.
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
Error: listen EACCES: permission denied 3000;
i add "PORT = 3000;" while "PORT = 3000" .
just semicolon";" give error
remove semicolon and project run successfully
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?
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')
});
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.

keep getting "? Something is already running on port 3000" when I do npm start on react app

I keep on getting "? Something is already running on port 3000" message in my terminal when I start up my react server when there is absolutely nothing running on my port 3000
What I have tried to solve with:
Restart the macOS.
checking my "http://localhost:3000" on chrome browser. (Nothing: This site can’t be reached)
Go to chrome://serviceworker-internals and chrome://appcache-internals, search for localhost:3000 (Nothing found)
I also tried almost every command lines I found on Google regarding this issue
I also created another express.js app, and it was successfully launch on port 3000 while React said there is something running on 3000. React on my computer just keeping thinking there is something running on port 3000.
If you are a windows user you can try using
npx kill-port 3000
in your console. I was having the same problem and it worked for me.
Not sure about MAC.
Open cmd and write this
netstat -a -o -n
You will get list of active connections then find 3000 by hitting
Cntrl + f
Copy the PID of that port and hit this command
taskkill /F /PID PID_of_port
Edit
This guide is for windows.
Kill Node.Js process from Task Manager.
Step 1: Open Task Manager by clicking ctrl+shift+delete
Step 2: Open Prcesses tab
Step 3: Search for Node.JS process and right click on that then click on End Task
Step 4: Now you can start again.
As #khurram khan suggested terminating the process may be the best option for you, This work for me on linux:
$ lsof -i tcp:3000
$ kill -9 PID
the first command should give you the PID number to enter in the second command as PID.
I had this problem on Mac and I solved by running:
npx kill-port 3000
I had stuck with this one for few hours, and the end I had found the solution
There was incorrect mapping to the local host in the hosts file and didn't had any relation to the port taken something like
10.2.224.130 localhost
Just Change it back to
127.0.0.1 localhost
Host file locations
https://www.howtogeek.com/howto/27350/beginner-geek-how-to-edit-your-hosts-file/
In case anyone comes across this and the above solutions didn't help, make sure your /etc/hosts has:
127.0.0.1 localhost
For some reason this was wiped from my hosts file and CRA's dependency for checking ports (detect-port-alt) checks localhost:[PORT] to see if it's available. If it errors out, you'll always get the "Something is already running..." error when checking a specific port but not when using a random port (since that's picked by the dependency).
kill node.js from the background processes. that would soleve the probelem. alterntively, you could restart your system
for window use this in package.json
"start": "set PORT=3006 && react-scripts start"
for Linux and MacOS
"start": "PORT=3006 react-scripts start"
This worked for me on windows pc. This one is for those are not seeing the port when you run this command netstat -a -o -n on your command prompt.
Open your command prompt in administrator mode and run this command
net stop winnat
you'll get this response:
The Windows NAT Driver service was stopped successfully.
Them you run this next:
net start winnat
then you will get this response:
The Windows NAT Driver service was started successfully.
once you do that. Start the react server and it would work. Same too if your backend server doesn't run on 3000
It's very simple. You can fix it in 2 easy steps.
Check your environment variables if there is a key/entry with name "PORT".
If found delete that entry or rename it to something else.
It turns out that some other program is using that variable. Usually when you start react-scripts it will look for an environment variable with that title PORT.
Date: Sat 07, October 2020
Windows: Microsoft Windows 10 Pro Build 19041
Node: 12.16.1
NPM: 6.14.8
Something similar was happening to me on my Windows machine. Tried a lot of things suggested here on StackOverflow and other places.
In my case, I was following a video course that was suggesting adding --script-version 1.1.5 (as illustrated below) when creating a new React app.
create-react-app app-name --scripts-version 1.1.5
Here's the package.json scripts versions:
React: 17.0.1
React DOM: 17.0.1
React Scripts: 1.1.5
After running the command npm start and typing y to try to use a different port, the terminal will stay stucked until it was canceled.
Then I created a new React app without --scripts-version 1.1.5 and after trying to running it, it was still running into Something is already running on port 3000 but after typing y, the app will run with no problem on a different port.
Here's the package.json scripts versions (when it was running successfully):
React: 17.0.1
React-Dom: 17.0.1
React-Scripts: 4.0.0
Starting with the reason you do not see anything on localhost:3000, is because there must be a proper webapp or a website/server running on that port, but the port:3000 is currently running some process, just not of a server/site/app.
This out of the way, depending on your type of OS, it is quite easy to check if there is in-fact a process on port:3000 or not. For MacOS, opening a terminal and running sudo lsof -i ':3000' will list the current process on port 3000.
Note that we need to run command as sudo or root
Once you see what process is running, note the PID and run kill -9 {PID}, which should kill the process with PID (i.e distruptive process on port:3000). Now doing a npm start should get your app built, compiled and served on localhost:3000
Mostly what might've happened is that you had a npm start running which you either interrupted, or closed in the foreground. Doing a fg in your terminal will open any background tasks, so if this opens a npm start instance, it was this process stopping your flow. You can then properly end the session making sure all processes on port:3000 are killed.
Happy coding
If you are on linux you can try
pkill -f node
to terminate the processs
The error occurs when a task is left running on the port properly killing it.
this error can occur on both ports. Your react app and your node server.
To solve this you can run the following commands.
netstat -anp tcp | grep 3000
This command in the terminal will give you the list of activities on port 3000
npx kill-port 3000
This command will kill all the running servers on port 3000.
Now if you have the same problem for your Node server. You can follow the same steps.
netstat -anp tcp | grep 5000
Then
npx kill-port 5000
You don't need the first command. it's just to list out the running activities on the port.
just change the port number for any other port number.
This is the output you get after running the commands.
I am using Git bash on windows.
For me, this works every time (macOS): sudo kill -9 $(sudo lsof -t -i:3000)
Simple and Easy solution
close your current terminal and open a new one.
If you are running it in VS Code just create a new terminal and delete the old one.
On React - you can run an already created React single-page application (SPA) by
npm start command.
That may start your locally hosting development server and runs your app at:
http://localhost:3000/ which is equivalent to: 127.0.0.1:3000 address
127.0.0.1 is the default localhost IP number while the default port number set by
create-react-app package is 3000.
When getting: “Something is already running on port 3000" failure error message you may think that the port captured by another process running on your machine but you’ll find that it is captured permanently as if it runs on 0.0.0.0:3000 address
Solution:
In your project libraries created by create-react-app script navigate to:
node_modules/react-scripts/scripts/start.js
While running npm start command - the start.js script is being called and executed
There at start.js file in you editor find the above line:
const HOST = process.env.HOST || '0.0.0.0';
and change it to:
const HOST = process.env.HOST || '127.0.0.1';
save and run your web app again at: http://localhost:3000/ or http://127.0.0.1:3000

Meteor deployed on server, but browser says site can't be reached

So I've deployed my meteor app, and have it up and running on an instance.
I've used the following environment variables:
MONGO_URL='mongodb://localhost:27017/meteor'
ROOT_URL='http://<my static ip>'
PORT=3000
And I run the program using the following command:
node bundle/main.js
It prints my "Meteor is starting up" that is printed using the console.log command, and then doesn't error out, but when I navigate to http://< my static ip >:3000 in a browser, I get an ERR_CONNECTION_REFUSED result.
My open mongod terminal says it's connecting fine to the MongoDB database.
Does anyone have any ideas on how to start debugging this issue?
Thanks.
In server you don't need to run meteor application on port 3000. You can run it on port 80 if the port is not being used by any other program.
If you are using port 80 make sure port 80 is opened by the network security rules.
If you are using port 3000 or any other port you will have to make sure that port is opened by the network security rules as above. Additionally you will have to mention the IP in your url, like http://<your_ip>:<port>

NodeJS OSX Unhandled 'error' event

So I'm creating a server with: const server = require('http').createServer();
Creating const io const io = require('socket.io')(server);
And doing server.listen();
var port = 4000;
server.listen(port);
But I'm getting this error and after a few hours searching and trying things I'm here on stackoverflow to ask here.
Things I have tried include:
Clearing port by using lsof -i tcp:4000 to get process but it didn't return anything,
Restarting computer,
Changing port,
Reinstalling node,
Updating node and,
Running as administrator
It used to work, but after restarting my computer the next day this happened.
Thanks for your help.
The error code is EPERM which means your port 4000 is locked. You can find the process causing it by using the command below for OSX El Capitan and above.
lsof -i tcp:4000
Turns out the problem was that Norton was blocking Node so I had to remove Node from the Norton application allowing list and re-add it.

Node.js EACCES error when listening on http 80 port (permission denied) [duplicate]

This question already has answers here:
Best practices when running Node.js with port 80 (Ubuntu / Linode) [closed]
(4 answers)
Closed 8 years ago.
Node.js throws following error while running on http port 80 (default port):-
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 figured out that node needs to have root access.
Conventionally we avoid giving root access in normal situation. What's the best practices for using it on port 80 (or port<1024).
This link has the same question but it has only one answer i.e. PREROUTING. While my solution provides other ways as well.
I am writing this to have all answers at one location, as I have to go thorough other resources than PREROUTING. Why not all answers at one location for sharing the knowledge
FYI: You cannot run socket on ports < 1024 with normal user permission. You need to have root access for it.
There are total 3 ways to solve the error:-
1. Give root access and run it (which is usual one)
2. Redirect to other port
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
Then launch my Node.js on port 3000. Requests to port 80 will get mapped to port 3000.
You should also edit your /etc/rc.local file and add that line minus the sudo. That will add the redirect when the machine boots up. You don't need sudo in /etc/rc.local because the commands there are run as root when the system boots.
Reference Link
3. Give Normal user capability of using sockets as root
Objective:- We are not providing full root access and only giving socket_root permission to access it by normal user to run your server on any port.
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://localhost.
Unfortunately, unless you sign on as root, you’ll normally have to use a URL like http://localhost:3000 - notice the port number.
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.
Reference Link
General Info Reference link from apache

Categories