ian
ian

Reputation: 12335

Node.js Error: connect ECONNREFUSED

I am new to node and running into this error on a simple tutorial.

I am on the OS X 10.8.2 trying this from CodeRunner and the Terminal. I have also tried putting my module in the node_modules folder.

I can tell this is some kind of connection problem but I have no idea why?

events.js:71
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: connect ECONNREFUSED
    at errnoException (net.js:770:11)
    at Object.afterConnect [as oncomplete] (net.js:761:19)

app.js:

var makeRequest = require('./make_request');

makeRequest("Here's looking at you, kid");
makeRequest("Hello, this is dog");

make_request.js:

var http = require('http');

var makeRequest = function(message) {

    //var message = "Here's looking at you, kid.";
    var options = {
        host: 'localhost', port: 8080, path:'/', method: 'POST'
    }

    var request = http.request(options, function(response) {
        response.on('data', function(data) {
            console.log(data);
        });
    });
    request.write(message);
    request.end();
};

module.exports = makeRequest;

Upvotes: 131

Views: 794891

Answers (22)

CR Rollyson
CR Rollyson

Reputation: 1581

I experienced this same error when my URL wasn't setup properly. Seems like a no brainer, but worth ensuring the URL you are constructing and passing is correct/what you want.

Upvotes: 0

OXiGEN
OXiGEN

Reputation: 2399

In my case, ECONNREFUSED was triggered by the SSL cert expiring for the smtp domain. Renewing the cert on the VPS fixed the issue on my VPS with the live domain.

However, my dev setup on localhost using the same smtp server failed because Malwarebytes was blocking the outgoing request without showing any warnings. When I tried to ping the smtp server from a cmd window, I got a General failure. response and now Malwarebytes popped up saying it was blocking a malicious site. After adding the smtp domain to Malwarebyte's allow list, the email sent successfully from localhost.

On a related note, I tried switching ports (465, 587) while troubleshooting. Using the wrong port (after the SSL and whitelist fixes) resulted in ETIMEDOUT errors instead.

Upvotes: 1

Hamid Reza Sedigh
Hamid Reza Sedigh

Reputation: 63

In NodeJs version > 17, the debugger bind IP is on IPv6, so first you should map your IP to IPv6 then use that in your program.

Upvotes: 4

Ronnie Smith
Ronnie Smith

Reputation: 18535

The name provided to Docker is the hostname to use when connecting to that host. For example, if your service is named "couchdb" you could access it from another container like "curl couchdb:5984".

https://docs.docker.com/compose/networking/

When you run docker compose up, the following happens:

  1. A network called myapp_default is created.
  2. A container is created using web’s configuration. It joins the network myapp_default under the name web.
  3. A container is created using db’s configuration. It joins the network myapp_default under the name db.

Upvotes: 0

Alistair St Pierre
Alistair St Pierre

Reputation: 11

For me this happened when my .env file was named incorrectly to .env.local

Upvotes: 1

Steven Shi
Steven Shi

Reputation: 1231

If you are using NodeJs version > 17, try to downgrade to use NodeJs 16. On 17, by default, the debugger bind IP is on IPv6, and sometimes you IDE might not be able to attach to it

Upvotes: 4

Javed A. Baloch
Javed A. Baloch

Reputation: 1070

In my case I forget to npm start the node app

Upvotes: 1

Vladimir Lazar
Vladimir Lazar

Reputation: 329

I just had this problem happen to me and after some searching for an answer I have found this other stackoverflow thread: ECONNREFUSED error with node.js that does not occur in other clients

The solution provided there, worked for me like a charm, it seems nodeJS has some issues accessing localhost dns, however switching to 127.0.0.1 works perfectly fine.

Upvotes: 21

KorbenDallas
KorbenDallas

Reputation: 1044

For me it was a problem in library node-agent-base v4.x which was requested by https-proxy-agent which in it's side was requested by newrelic v5.x library.

The problem was that node-agent-base v4.x for some marvellous idea decided to patch core https.get request (You can see this in the file patch-core.js in the repo). And when I used library which use https.get, it was failed because node-agent-base v4.x have changed function signature, which gave me request to 127.0.0.1 as initial url was lost...

I've fixed this by updating to the next version of newrelic 6.x, where node-agent-base v4.x doesn't has such 'patches'.

This is just crazy...hope my response will save you some time, I've spent several days to debug this.

Upvotes: 2

E. Djalalov
E. Djalalov

Reputation: 11

I was having the same issue in my dev env with Postman when checking the API. I searched literally all the answers came from google, but no avail.

SOLUTION: I have just moved to "insomnia" (an alternative to Postman) and it works just fine. Hope this workaround may help.

Upvotes: 0

Yuvaraj Anbarasan
Yuvaraj Anbarasan

Reputation: 657

ECONNREFUSED error means that connection could not be made with the target service (in your case localhost:8080). Check your service running on port 8080.

To know more about node.js errors, refer this doc.

Upvotes: 7

Ricardo Mitchell
Ricardo Mitchell

Reputation: 31

Had a similar issue, it turned out the listening port printed was different from what it actually was. Typos in the request string or listening function might make the target server appear to not exist.

Upvotes: 1

shamila
shamila

Reputation: 1330

Run server.js from a different command line and client.js from a different command line

Upvotes: 2

Bob Vargas
Bob Vargas

Reputation: 139

If you are on MEAN (Mongo-Express-AngularJS-Node) stack, run mongod first, and this error message will go away.

Upvotes: 7

SmujMaiku
SmujMaiku

Reputation: 643

The Unhandled 'error' event is referring not providing a function to the request to pass errors. Without this event the node process ends with the error instead of failing gracefully and providing actual feedback. You can set the event just before the request.write line to catch any issues:

request.on('error', function(err)
{
    console.log(err);
});

More examples below:

https://nodejs.org/api/http.html#http_http_request_options_callback

Upvotes: 1

pashaplus
pashaplus

Reputation: 3706

Sometimes it may occur, if there is any database connection in your code but you did not start the database server yet.

Im my case i have some piece of code to connect with mongodb

mongoose.connect("mongodb://localhost:27017/demoDb");

after i started the mongodb server with the command mongod this error is gone

Upvotes: 6

Andrew M.
Andrew M.

Reputation: 101

You need to have a server running on port 8080 when you run the code above that simply returns the request back through the response. Copy the code below to a separate file (say 'server.js') and start this server using the node command (node server.js). You can then separately run your code above (node app.js) from a separate command line.

var http = require('http');

http.createServer(function(request, response){

    //The following code will print out the incoming request text
    request.pipe(response);

}).listen(8080, '127.0.0.1');

console.log('Listening on port 8080...');

Upvotes: 8

mujaffars
mujaffars

Reputation: 1479

Check with starting mysql in terminal. Use below command

mysql-ctl start

In my case its worked

Upvotes: 2

Siddhartha Mukherjee
Siddhartha Mukherjee

Reputation: 6228

I was having the same issue with ghost and heroku.

heroku config:set NODE_ENV=production 

solved it!

Check your config and env that the server is running on.

Upvotes: 11

user2957009
user2957009

Reputation: 933

Chances are you are struggling with the node.js dying whenever the server you are calling refuses to connect. Try this:

process.on('uncaughtException', function (err) {
    console.log(err);
}); 

This keeps your server running and also give you a place to attach the debugger and look for a deeper problem.

Upvotes: 74

JakubKnejzlik
JakubKnejzlik

Reputation: 6483

You're trying to connect to localhost:8080 ... is any service running on your localhost and on this port? If not, the connection is refused which cause this error. I would suggest to check if there is anything running on localhost:8080 first.

Upvotes: 11

rahulmehta95
rahulmehta95

Reputation: 568

People run into this error when the Node.js process is still running and they are attempting to start the server again. Try this:

ps aux | grep node

This will print something along the lines of:

user    7668  4.3  1.0  42060 10708 pts/1    Sl+  20:36   0:00 node server
user    7749  0.0  0.0   4384   832 pts/8    S+   20:37   0:00 grep --color=auto node

In this case, the process will be the one with the pid 7668. To kill it and restart the server, run kill -9 7668.

Upvotes: 4

Related Questions