Connecting node to mongodb getting error from node module

I have tested the code with a localhost and it works fine however when I try to test the connection on the server I get the error

        this.options = options ?? {};

The process is npm init and give the project a name npm install mongodb then run node file, it seems like the error is on the package as when I run node without the MongoClient the node runs and running this script (locally on a window machine the server is linux) it works even with the same ip address.
Here is the code I use for the connection

const { MongoClient } = require("mongodb").MongoClient;
const http = require('http');
const hostname = '';
const port = 3000;
const uri ="mongodb://"SERVER IP ADDRESS":27017/";
const client = new MongoClient(uri);
const server = http.createServer((req, res) => 
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/html');
    async function run() 
            await client.connect();
            const db = client.db("preworn_market");
            const specifics = db.collection("product_specifics");
            const cursor = specifics.find();
            await cursor.forEach(function(myDoc) {  res.write("<h4>"+myDoc.category+"</h4>"); });
            await client.close();


server.listen(port, hostname, () => 
  console.log(`Server running at http://${hostname}:${port}/`);

Hi :wave: @Aneurin_Jones,

Welcome to the MongoDB Community forums :sparkles:

Looking at this line, it seems that there is a syntactical error.

I’ll recommend modifying it to:

const uri = `mongodb://${SERVER_IP_ADDRESS}:27017/`;

Alternatively, if the value is being fetched from the .env file:

then please modifies it to:

const uri = `mongodb://${process.env.SERVER_IP_ADDRESS}:27017/`;

I hope it helps!


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.