116

Optional chaining (obj?.param1?.param2) seems to be a great feature and I really wanted to see it implemented and finally get rid of nested ifs, arbitrary functions and what not for such a simple operation.

But there's a problem, it doesn't work. I updated to Node 12 and I still get an error:

var dude = res?.param?.params[0]
SyntaxError: Unexpected token '.'

or

var dude = res.param?.params[0]
SyntaxError: Unexpected token '.'

What is the problem?

Do I need to change some language config or download a library to enable this feature? Or is it simply not out yet?

Sangwin Gawande
  • 7,658
  • 8
  • 48
  • 66
almarc
  • 1,598
  • 3
  • 11
  • 31
  • 5
    It is in `Stage 4`. Node.js doesn't support this yet. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining – Sridhar Jan 03 '20 at 06:34
  • 1
    I searched for 'nodejs null propagation' and initially couldn't find this question. Hopefully this comment will improve searchability. – Janosh Apr 07 '20 at 12:17
  • I'm afraid I can't do much about it. There's a tag for it, but it's not related to Javascript and will cause more confusion than do good. – almarc Apr 07 '20 at 15:40
  • 6
    Optional chaining is now supported in node version v14.2.0. Update! – allez l'OM May 19 '20 at 13:49

5 Answers5

155

Optional chaining is currently not supported in Node.js version 13 and below. It will be supported from Node.js version 14 and most of the browsers as it is moved to Stage 4. Currently, few platforms are supporting it. You can find the list of platforms supporting optional chaining in the given link. You can enable optional using --harmony flag.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Balaj Khan
  • 2,490
  • 2
  • 17
  • 30
  • 5
    Jan 2021: This is still an issue in codesandbox website which takes the official docker image for nodejs. I had to use `result && result.insertedCount === 1 ? { data: "success" } : { error: "fail" }` – Manohar Reddy Poreddy Jan 26 '21 at 11:20
  • 1
    If you're using esm, optional chaining won't work on Node 14+ until this ticket was fixed https://github.com/standard-things/esm/issues/866 – Cartucho Sep 03 '21 at 21:37
28

The spec for the optional chaining feature was just promoted to Stage 4 (Finished) on December 22, 2019. Node 12 came out before the spec was final - and so did Node 13, for that matter.

According to node.green, optional chaining will be supported starting with Node 14, but will still require the --harmony flag. (This seems to conflict with Node's description of the --harmony flag - V8's shipping features aren't supposed to require the flag - so I'm not sure what to make of that.) Still, whether it needs a flag or not, I wouldn't expect to see the feature until the Node 14 release around April 2020.

If you want to play with optional chaining today, your best bet is to use TypeScript (which added optional chaining in version 3.7) or a preprocessor like Babel.

Joe White
  • 94,807
  • 60
  • 220
  • 330
16

I was able to use nodejs v13.7.0 with --harmony flag.

node --harmony myCode.js

Dinah

undefined

undefined

//myCode.js

const adventurer = {
  name: 'Alice',
  cat: {
    name: 'Dinah'
  }
};

const catName = adventurer.cat?.name;
console.log(catName);
// expected output: Dinah
const dogName = adventurer.dog?.name;
console.log(dogName);
//expected output: undefined

console.log(adventurer.someNonExistentMethod?.())
//expected output: undefined
Community
  • 1
  • 1
user3283069
  • 355
  • 1
  • 6
4

Optional Chaining will be implemented with Node.js v14, which will be released on 20/04/2020. By now, you may use Babel with @babel/plugin-proposal-optional-chaining.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Chema
  • 147
  • 2
  • 9
0

If you're still having this issue, check the node version you're using node --version.

If you have nvm, make sure you're using a node version that implements the operator that is giving an error.

e.g.

nvm install 15.8
nvm use 15.8
lele
  • 442
  • 6
  • 14
  • 1
    The OP asked how to use it in Node 12, and not how to install Node 15.8. – kravemir Nov 29 '22 at 10:13
  • the suggestion was if the OP was still having the issue, to check the node version and change it with nvm if necessary :) – lele Jan 24 '23 at 13:48