NGROK RequestError: connect ECONNREFUSED 127.0.0.1:4040

670 Views Asked by At

I occasionally encounter the following error while setting up the ngrok tunnel. Any ideas on how to resolve it?

RequestError: connect ECONNREFUSED 127.0.0.1:4040
    at ClientRequest.<anonymous> (C:\Users\user\Downloads\Node\RequestLogger_Dev\node_modules\got\dist\source\core\index.js:970:111)
    at Object.onceWrapper (node:events:629:26)
    at ClientRequest.emit (node:events:526:35)
    at origin.emit (C:\Users\user\Downloads\Node\RequestLogger_Dev\node_modules\@szmarczak\http-timer\dist\source\index.js:43:20)
    at Socket.socketErrorListener (node:_http_client:495:9)
    at Socket.emit (node:events:514:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) {
  code: 'ECONNREFUSED',
  timings: {
    start: 1702548022231,
    socket: 1702548022232,
    lookup: undefined,
    connect: undefined,
    secureConnect: undefined,
    upload: undefined,
    response: undefined,
    end: undefined,
    error: 1702548022233,
    abort: undefined,
    phases: {
      wait: 1,
      dns: undefined,
      tcp: undefined,
      tls: undefined,
      request: undefined,
      firstByte: undefined,
      download: undefined,
      total: 2
    }
  }
}

NGROK Error

Code:

// Asynchronous function to set up an ngrok tunnel
async function setupNgrokTunnel() {
    try {
        // Connect to ngrok, providing the port, authentication token, and disabling TLS binding
        const url = await ngrok.connect({ addr: config.PORT, authtoken: config.AUTH_TOKEN, bind_tls: false });

        // Log the public URL of the ngrok tunnel
        console.log('\nNgrok Tunnel URL:', url);

        // Update the server's endpoint in the configuration with the ngrok tunnel URL
        config.ENDPOINT = url;

        // Close the ngrok tunnel when the application exits
        process.on('SIGINT', async () => {
            await ngrok.disconnect();
            console.log("close...........")
            process.exit();
        });
    } catch (err) {
        // Handle errors during ngrok tunnel setup and log the details
        console.error('\nError creating ngrok tunnel:', err);

        // Update the configuration with the error details
        config.ERROR = err;
    }
}

Code

1

There are 1 best solutions below

0
Ying On

Maybe check out if there is any process already running ngrok in background.

In my case, I use Run Code in VSCode to run ngrok.connect() and then I executed it agian in terminal. This resulted in the same error you mentioned.