Not able to connect to database from the VS Code

Not able to connect with database getting this error

C:\Users\DELL\Desktop\MERN Ecommerce\node_modules\mongodb\lib\operations\add_user.js:16
        this.options = options ?? {};
                                ^
SyntaxError: Unexpected token '?'
    at wrapSafe (internal/modules/cjs/loader.js:1070:16)
    at Module._compile (internal/modules/cjs/loader.js:1120:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
    at Module.require (internal/modules/cjs/loader.js:1042:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> (C:\Users\DELL\Desktop\MERN Ecommerce\node_modules\mongodb\lib\admin.js:4:20)
    at Module._compile (internal/modules/cjs/loader.js:1156:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)

ANd this is the code

const mongoose = require("mongoose");

const connectDatabase = async () => {
    try {
        await mongoose.connect(`${process.env.DB_URI}`, {
            useNewUrlParser: true,
            useUnifiedTopology: true,
        });
        console.log('MongoDB connected');
    } catch (error) {
        console.log(error.message);
        process.exit(1);
    }
};

module.exports = connectDatabase;

Hi :wave: @shrikant_kumar1,

Welcome to the MongoDB Community forums :sparkles:

The following error is occurring due to the compatibility issue with the Nullish coalescing operator (??) which is introduced in node version 14.0.0 and above. Please refer to mdn web docs for version compatibility.

Here, the internal MongoDB node module package is utilizing this operator, resulting in the error.

Here, Nullish coalescing operator (??) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.

Although, I presume that you are currently using a node version below 14.0.0. I suggest upgrading at least to version 14.20.1 and above which is also the new minimum supported Node.js version by MongoDB Node.js Driver v5 and then reinstalling the node_modules packages. This should hopefully resolve any issues you are experiencing.

As a point of reference, I attempted to replicate your error on my own environment by running the provided code with a node version below 14.0.0 and encountered a similar error.

[10:37:31] ➜  ~ node --version  
v12.13.0
[10:38:35] ➜  ~ node test.js    
/Users/kushagra/node_modules/mongodb/lib/operations/add_user.js:16
        this.options = options ?? {};
                                ^

SyntaxError: Unexpected token '?'
    at Module._compile (internal/modules/cjs/loader.js:892:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Module.require (internal/modules/cjs/loader.js:849:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/Users/kushagra/node_modules/mongodb/lib/admin.js:4:20)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)

After upgrading the node version to 14.0.0 or higher, it works as expected.

[10:38:42] ➜  ~ node --version
v14.20.0
[10:39:20] ➜  ~ node test.js  
Connected to MongoDB

I hope it helps. Let us know if you have any further questions.

Regards,
Kushagra

3 Likes

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