Home > Enterprise >  can't use optional chaining (?)
can't use optional chaining (?)

Time:09-29

I try to use optional chaining in my nodeJS project.

I use version 15.8 (or any >12, I'm using nvm).

my project is run with flag esm

$ run.env -p ../../.env nodemon -r esm --watch ../../.env --watch ./src/ ./src/index.js
[nodemon] 2.0.12
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): ../../.env src/**/*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node -r esm ./src/index.js`
/home/dre/Projects/my/packages/backend/src/routes/account/login.js:72
    let user = await Account.findOne({ email: payload?.email });
                                                      ^

SyntaxError: Invalid or unexpected token
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
[nodemon] app crashed - waiting for file changes before starting...

My eslint is not yelling at this syntax anymore, but no way to have it understood for node.

Any idea would help me a lot!

CodePudding user response:

You have to enable this feature as it is not widely supported by applying the --harmony flag which applies the new ECMAScript 6 features.

According to node.green, optional chaining will be supported starting with Node 14, but will still require the --harmony flag.

CodePudding user response:

Updated to v16.10 removed the esm flag put "type": "module" in my package.json

I now need to adapt my app a bit to make it work and fix some new errors.

Thx for your help anyway :)

  • Related