skip to Main Content

I have this error while using mongoose:

/var/www/node-api/node_modules/mongoose/node_modules/mongodb/lib/admin.js:62
            session: options?.session,
                             ^

SyntaxError: Unexpected token '.'

I reinstalled node_modules but I always get the same problem.

For mongoose and my connection in my device it worked correctly.

2

Answers


  1. Adding an answer for this because it keeps coming up.

    Problem:

    Mongoose uses the MongoDB native Node.js driver as one of it’s dependencies.

    In the mongodb V6 native driver they implemented optional chaining in the async command() within admin.js:

    async command(command, options) {
       return (0, execute_operation_1.executeOperation)(this.s.db.client, new run_command_1.RunAdminCommandOperation(command, {
          ...(0, bson_1.resolveBSONOptions)(options),
          session: options?.session, //< optional chaining here
          readPreference: options?.readPreference
       }));
    }
    

    The problem is that optional chaining wasn’t supported in Node.js until V14.5.

    Solution:

    You need to upgrade your Node.js version to a newer version. At the current time of writing, Node V20 is the LTS so I would reccomed upgrading to that or see the Release Schedule to plan ahead.

    Login or Signup to reply.
  2. ok, so what is the answer to fix this? I have node v21 and still getting this error

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search