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());
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.
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.
@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.
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 .
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.
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.
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.