Node app - SyntaxError: Unexpected token

Getting this error when trying to start my server and connect to mongodb.
I have Node v21.1.0 installed. Anyone fixed this error before?

node_modules\bson\lib\bson.cjs:479
        inspect ??= defaultInspect;
                ^^^

SyntaxError: Unexpected token '??='
4 Likes

I was facing the same issue.

As a workaround, I integrated docker to my NodeJS application and used the latest stable version of Node (18.12.0). That solved the problem so I am guessing the issue stems from using Node v21

1 Like

I don’t think so. I was using v18 and still had the same issue. I read another post that said upgrading node to the latest was the fix. It wasn’t.

@wallinglee we saw a similar question when 6.0.0 of the driver was released (MongoDB NodeJS Driver 6.0.0 Released - #3 by Warren_James) .

The nullish coalescing assignment has been supported since Node.js 15, so it would be worth verifying that you’re indeed running your code in the version of the runtime you’ve indicated.

If you add a console.log(process.version) to the code this should be fairly easy to validate.

Nothing logs because the error happens.

If you tried the same test using Node.js 21 (via docker) does the issue still occur?

I’m not using docker

node -v
v18.12.0

console.log(process)

process:  process {
  version: 'v14.18.0',
  versions: {
    node: '14.18.0',

@wallinglee, based on your process output you’re still running Node 14 - even if you have Node 18 available.

I’m not sure how you’re starting your server, but it appears an older version of Node is being used here.

I just use npm run start in my node express app.

"start": "node index.js"

Yeah, it appears to be a node version error. I switched from v14.21.3 to v18.2.0 and then it worked fine.

1 Like

I’ve tried several node version installs. None fix the issue

Its not a Node Issue, its issue with mongoose V-8 and above, if you use mongoose V-7.4.1 then problem will be solve.

3 Likes

I’m getting a similar error. Tried downgrading mongoose to the version you specified, but it keeps happening.
Any workaround?

SyntaxError: Unexpected identifier

I was facing the same issue and after trying different approaches, the one that worked for me was using mongoose version 7.6.3

So in your server, run the command: npm i mongoose@7.6.3
It worked for me. Hopefully, it does for you too :slight_smile:

4 Likes

Try using mongoose version 7.6.3 instead of the latest one. It worked for me :>

Thank’s! It’s work for me to!

Moving to version 7.6.3 worked ! Thanks

The version 8 was working locally for me but when I tried to deploy to render it didnt work

But 7.6.3 worked thanks!

PT-BR: Isso deu certo obrigado
EN: this worked thank you

Moving to version 7.6.3 it worked! Thanks