Last updated: Apr 5, 2024
Reading timeยท4 min
The Node.js Error: connect ECONNREFUSED occurs when a connection to the server could not be established.
To solve the error, make sure your application or database server is started and doesn't throw any errors before it's able to respond to HTTP requests.
UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED http://localhost:8000 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16) (Use `node --trace-warnings ...` to show where the warning was created) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
The first thing you should try is to add the following 3 lines of code at the
top of the entry file of your Node.js server (e.g. index.js
).
process.on('uncaughtException', function (err) { console.log(err); });
The code simply handles uncaught exceptions and logs them to the terminal.
This would prevent your server from crashing before it's able to respond.
It is also useful for debugging because it logs uncaught exceptions to the terminal.
Here is an example of a simple Express, Node.js server.
const express = require('express'); const app = express(); // ๐๏ธ Handle uncaught exceptions process.on('uncaughtException', function (err) { console.log(err); }); app.get('/', (req, res) => { res.json('Hello World!'); }); const port = 3445; app.listen(port, () => { console.log(`Example app listening on port ${port}`); }); throw new Error('An error occurred');
I can start the server with the node index.js
command.
node index.js
Now I can make HTTP requests to http://localhost:3445
.
process.on()
event handle handles the error, so the server doesn't crash instantly.The error most often occurs for the following reasons:
http://
or https://
) in the URL.If you specify an incorrect URL or PORT, you won't be able to establish a connection and the connect ECONNREFUSED is raised.
Here is an example HTTP request using fetch
.
async function getData() { try { const response = await fetch('http://localhost:3445'); if (!response.ok) { throw new Error(`Error! status: ${response.status}`); } const result = await response.json(); console.log(result) return result; } catch (err) { console.log(err); } } getData()
The getData
function makes an HTTP request to http://localhost:3455
(port
3455).
http
or https
) and ensure the endpoint and the PORT are correct.The error is often raised due to connectivity issues caused by specifying an incorrect URL or PORT.
Another common cause of the error is forgetting to start your application or database server.
You can start your application server in a separate terminal window before making HTTP requests.
Make sure that your database is running if you're trying to connect to a database.
For example, if you use MongoDB, start your Mongo server first and then your Node.js server.
If you are testing on localhost, try to replace http://localhost:3000
with
http://127.0.0.1:3000
(adjusting your port).
Here is an example.
async function getData() { try { // ๐๏ธ Replaced http://localhost with http://127.0.0.1 const response = await fetch('http://127.0.0.1:3445'); if (!response.ok) { throw new Error(`Error! status: ${response.status}`); } const result = await response.json(); console.log(result) return result; } catch (err) { console.log(err); } } getData()
Make sure the port you have started your server on is not taken by a different application.
This would cause connectivity issues and the Node.js connect ECONNREFUSED error.
Make sure that a firewall is not preventing you from reaching the server.
Open your terminal in your project's root directory (where your package.json
)
file is located and then run the npm install
command.
npm install
If the error is not resolved, try to delete your node_modules
and
package-lock.json (not
package.json
) files, re-run npm install
and restart your IDE.
# ๐๏ธ (macOS/Linux) delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json # ๐๏ธ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json # ๐๏ธ clean your npm cache npm cache clean --force # ๐๏ธ install packages npm install
If the error persists, try to restart your IDE and development server.
Visual Studio Code often glitches and needs a reboot.
To solve the error "Node.js Error: connect ECONNREFUSED", make sure:
http://
or https://
) in
the URL.