node.jsamazon-web-servicesmongoosesyntax-error

( session: options?.session, ^ SyntaxError: Unexpected token '.')mongoose give a error deploying in instance ubuntu aws


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.


Solution

  • 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 recommend upgrading to that or see the Release Schedule to plan ahead.