Error:
SyntaxError: Unexpected token '?'
Here is the code:
const result1 = (null || undefined) ?? 'OK';
console.log(result1); // 'OK'
Error:
SyntaxError: Unexpected token '?'
Here is the code:
const result1 = (null || undefined) ?? 'OK';
console.log(result1); // 'OK'
It raises the unexpected token error when I run this program in Node.js
- What exactly is the error? – Wais Kamal Commented Dec 21, 2020 at 16:58
- 2 You're using too old a version. If my memory serves me correctly, it was added in node 14. – Aplet123 Commented Dec 21, 2020 at 16:58
- 2 This was added in Node.js 14.0.0. What version are you running? – Barmar Commented Dec 21, 2020 at 16:58
- 1 The first place you should always look when you get an error involving a new language feature is the patibility chart in MDN. developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Barmar Commented Dec 21, 2020 at 16:59
- 1 Code Snippets are executed in the browser of whoever reads the question. Trying to demonstrate failures that are particular to your specific environment by running code in the reader's environment doesn't make much sense. – Jörg W Mittag Commented Dec 21, 2020 at 17:03
2 Answers
Reset to default 7The nullish coalescing operator was newly added in ECMAScript 2020, in June 2020 (about six months ago as of the date of the original question).
If you are using ECMAScript 2020 features in your code, make sure you use an ECMAScript implementation that fully implements ECMAScript 2020 (or at least the features you are using).
Assuming you are on Windows, download the LTS or current.
After this reboot your machine and make sure in your package json you do not specify an engine
that is less than 13 (optional)
{ "engines" : { "node" : ">14" } }
run your script