Node.js Error: connect ECONNREFUSED

466,465

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

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

Share:
466,465

Related videos on Youtube

ian
Author by

ian

Updated on February 01, 2022

Comments

  • ian
    ian 9 months

    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;
    
    • ian
      ian almost 10 years
      I also put this code up on https://c9.io/ and get the same error.
    • ian
      ian almost 10 years
      Open for incoming our out coming or what? Same issue on a node server using the service defined ports and host. If you actually know what is going on here post a real solution not vague comments as stated in the question I am new to node.
    • Brad
      Brad almost 10 years
      @ian, Nowhere in your cod are you creating a server. So, what do you have running on port 8080 that you are trying to connect to?
    • Michael J. Calkins
      Michael J. Calkins almost 9 years
      For any future readers I was trying to make an API request to a PHP server and my request port was not set to 80. Once changed my http requests worked fine.
    • Joshua Pinter
      Joshua Pinter 9 months
      I was running node 17 (on a M1 Mac) and I removed it via brew uninstall node and then install node 16 with brew install [email protected]. Worked a treat. I may have had to brew link --force [email protected] but I can't remember right now.
  • Brad
    Brad almost 10 years
    That's not the problem here. He's getting connection refused when trying to make a request.
  • Martin Epsz about 8 years
    Keeping your server running after an uncaught exception is a dangerous thing to do. You do not know if the exception left your server in an expected state. It's much safer to kill the server and have another process restart it (and log the exception, obviously).
  • Brad
    Brad over 7 years
    Can you explain why this fixes the issue?
  • Winnemucca
    Winnemucca over 7 years
    I used heroku config:set NPM_CONFIG_PRODUCTION=false. I would like to know how that worked though
  • Yan Foto
    Yan Foto about 7 years
    In general its not a good idea to catch all exceptions in the dark. Take a look at this answer.
  • user2957009
    user2957009 about 7 years
    It depends on what you are trying to accomplish. I was writing a monitoring app where it is critical to keep up and keep moving. It seems the OP was struggling with just finding out what the issue was. All the other answers are about specific issues that could be the cause of this error. As a debugging step the above is almost always necessary before you can drill down to the real issue. It is also weird to me that you are OK with your server dying because of one failure, causing all other client calls to start failing?
  • Ahmad Ali over 2 years
    2 days of search and then I saw your comment, saved my day