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.';
}

main()
  .then(console.log)
  .catch(console.error)
  .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.
2 Likes

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.

3 Likes

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 127.0.0.1 instead, as suggested.

Econnrefused ::1:27017

Follow up of Econnrefused ::1:27017

2 Likes

With

const url = 'mongodb://127.0.0.1:27017';

it works fine.

Sample code is copied directly from official github site https://github.com/mongodb/node-mongodb-native
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 .

1 Like

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 (github.com)

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 127.0.0.1 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

3 posts were split to a new topic: TypeError: Object prototype may only be an Object or null: undefined

I’ve been facing this error from an hour,
what if we want to create multiple collections with multiple schemas?

Hi @Nayab_Rasool

It is not clear what your problem is and does not seem to relate to this topic.

Can you please give more details so we may have a better understanding!? and we maybe move it to a new discussion topic for broad attention.

5 posts were split to a new topic: Create a collection with node.js