MongoDb doesnt' work with node.js 18.12.1

os: win10;
node: 18.12.1
mongodb: “^4.11.0”

Program which worked with node.js 16 doesn’t work with node.js 18.
Even the simplest sample:

const { MongoClient } = require('mongodb');

// or as an es module:
// import { MongoClient } from 'mongodb'

// Connection URL
const url = 'mongodb://localhost:27017';
const client = new MongoClient(url);

// Database Name
const dbName = 'myProject';

async function main() {
  // Use connect method to connect to the server
  await client.connect();
  console.log('Connected successfully to server');
  const db = client.db(dbName);
  const collection = db.collection('documents');

  // the following code examples can be pasted here...

  return 'done.';

  .finally(() => client.close());

breaks with :
MongoServerSelectionError: connect ECONNREFUSED ::1:27017
at Timeout._onTimeout (D:\TEST\mongodb-test\node_modules\mongodb\lib\sdam\topology.js:293:38)
at listOnTimeout (node:internal/timers:564:17)
at process.processTimers (node:internal/timers:507:7) {
reason: TopologyDescription {
type: ‘Unknown’,
servers: Map(1) { ‘localhost:27017’ => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
code: undefined,
[Symbol(errorLabels)]: Set(0) {}

if it is only the nodejs version you have upgraded:

  • your server might have crashed for some reason and might not be working right now. please check server status.
  • library versions may also depend closely on nodejs versions. can you please also upgrade mongodb package (maybe uninstall then reinstall latest)? make a back up first.
1 Like

The error ECONNREFUSED means no server is listening at the given address and port.

The address mentioned

with the connection string

means localhost is defined for IPv6. A localhost IPv4 would be 127.0.01. You have to make sure your mongod is listening to IPv6 interfaces or use 127.0.01 in your connection string rather than localhost.


Wow! I completely missed that part.

@Francek_Prijatelj you may check 2 forum posts below if you have time for a similar issue.

As @steevej stated above, your localhost is bound to ::1 right now. It possibly is a recent change in your c:\Windows\System32\drivers\etc\ file (an upgrade or a new program) that activated a ::1 localhost line

You may remove/comment out it, or use instead, as suggested.

Econnrefused ::1:27017

Follow up of Econnrefused ::1:27017



const url = 'mongodb://';

it works fine.

Sample code is copied directly from official github site
and it works with node16.
I don’t think localhost is changed globally, as it wouldn’t work with node16 .
Maybe there is a regression or a change in node@18.12.1 .

When we develop an app, the first assumptions we made might not be the best. And that choice must become a more generic one at some point to support the future. even if it breaks things, this is the right way we should choose.

Node.js team made that decision change with this about a year ago: dns: default to verbatim=true in dns.lookup() by treysis · Pull Request #39987 · nodejs/node (

One of developers #richardlau has this response on an issue ticket here Unable to connect to establish a TCP connection (ECONNREFUSED) · Issue #40702 · nodejs/node · GitHub

For reference the change was #39987 – Node.js no longer re-sorts results of IP address lookups and returns them as-is (i.e. it no longer ignores how your OS has been configured). You can change the behaviour via the verbatim option to dns.lookup() or set the --dns-result-order command line option to change the default.

Respect the OS configuration lies at the heart of the change. whether this configuration change was made by users themselves intentionally or an app changes it.

Please check this for a priority discussion over IPv4 and IPv6
networking - IPv4 vs IPv6 priority in Windows 7 - Super User

1 Like

To avoid confusion, it would be helpful to change localhost to on the github page

could be. But using localhost is a tradition for decades as using names rather than numbers is preferred by us human beings.

Plus, IPv6 is relatively new and it is better to leave the job of dealing with problems that come together with using it to the users.

It is actually a problem caused by OS developers who choose to activate IPv6 to be “localhost” in the “hosts” file without making it clear that it might break running programs. I was shocked to see this problem when I had it for the first time.

Anyways, it is still a good exercise to show not everything goes to our expectations and we will have to dig deeper if we want to fix things. Fortunately, this one is not too deep to swallow us: use the number format, edit hosts file, or add flags.

1 Like