15

I am trying to work on my nodejs and reactjs app at school but everytime I run the server, I get an error with localhost:PORT/net::ERR_CONNECTION_REFUSED

Is the wifi refusing the connection on localhost?

PS: The react side renders correctly

blouuuu
  • 454
  • 1
  • 4
  • 16

10 Answers10

16

Try changing the url from localhost to 127.0.0.1 . If it works, then it means your environment is not resolving localhost to 127.0.0.1

Or Try with changing the PORT Number

Maulik Sakhida
  • 471
  • 4
  • 15
  • 5
    If you are on a mac, the issue could be you have two entries in the `/etc/hosts` file for localhost. When I encountered this issue, I had `127.0.0.1 localhost` and `::1 localhost` in my `/etc/hosts` file. Remove the `::1 localhost` line resolved the issue. – Alexander Aavang Oct 20 '22 at 13:28
  • 1
    @AlexanderAavang You just saved me big time! This totally escaped me for hours. Here I am swimming around in old commits trying to catch a ghost. – Kalnode Mar 07 '23 at 04:03
  • Does anybody know why Node isn't able to resolve the DNS name? Secondly, would this happen on the actual environment (dev on Azure)? Thank you for saving the day. – Rikki Aug 16 '23 at 05:31
15

If you're using WSL (linux on windows), restart its service LxssManager Servie (run services.msc) and try again.

6

For those of you that the approved solution didn't work for (changing localhost to 127.0.0.1) I was able to fix this just by restarting my computer. Not sure what caused the issue but the day before today my app was working fine and then today I got hit with this error. Restarting fixed the issue.

AJRohrer
  • 925
  • 9
  • 9
  • 1
    Yep I second this. I am using a window 10 devices and running npm through WSL and the code was working fine earlier in the day. When I came back, it say localhost refused to connect. A restart would definately help. – hoan duc Jun 04 '21 at 01:28
2

I was also facing this problem on mac m1 from last 2 days, so finally i ended fixing the issue by :-

  1. Run 'npm start' from default shell(/bin/bash) on vs code.
  2. You can also try to update the node version.
  3. You may need to change the port for application only if some other application is running on default port(3000)
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Wasit Shafi
  • 854
  • 1
  • 9
  • 15
2

In my case I have an error with require in setupProxy.js

Lucky_hunter
  • 486
  • 1
  • 5
  • 10
1

In my case, deleting the node_modules and then reinstalling using npm install worked.

Mohit Kushwaha
  • 1,003
  • 1
  • 10
  • 15
Adnan GM
  • 11
  • 2
1

In my case, I resolved the issue using "npm start" command again in terminal of vs code and it works.

0

In my case I had made changes to the setupProxy.js that caused the problem. Try reverting back to something that is working, such as:

const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function(app) {
  app.use(
    '/api',
    createProxyMiddleware({
      target: 'http://localhost:5000',
      changeOrigin: true,
    })
  );
};
ormurin
  • 358
  • 3
  • 11
-2

Rebooting the system (linux) worked for me. Consider rebooting first.

sai_varshittha
  • 193
  • 1
  • 12
-6

the problem of "localhost refused to connect." is occur because of command prompt that you closed so you must keep open your command prompt after hit "npm start" command till you work in "App.JS" file.

i'm sure this will work 100%.