I am very new in NodeJS and I have the following problem running a node application:
andrea@ubuntu:~/Documents/XXX/custom-dashboard-be$ npm run dev
> custom-dashboard-be@1.6.0 dev /home/andrea/Documents/XXX/custom-dashboard-be
> cross-env NODE_ENV=development nodemon src/index.js
[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node src/index.js`
/home/andrea/Documents/XXX/custom-dashboard-be/src/utils/esMapper.js:29
if(data.result && data.result.body && data.result.body?.aggregations["2"]?.buckets.length > 0) {
^
SyntaxError: Unexpected token .
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/home/andrea/Documents/XXX/custom-dashboard-be/src/controllers/elastic.controller.js:5:109)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
[nodemon] app crashed - waiting for file changes before starting...
The error seems on the line 29 of this esMapper.js class, that is the first if statment of this method:
const esSudoEventsAndHostnamesMapper = async (data) => {
if(data.result && data.result.body && data.result.body?.aggregations["2"]?.buckets.length > 0) {
let aggregations = data.result.body.aggregations["2"].buckets;
aggregations.map((event) => {
let totalCount = event.doc_count;
event["3"].buckets.map(nestedEvent => {
nestedEvent.percentage = ((nestedEvent.doc_count / totalCount) * 100).toFixed(2);
})
})
data.result.body.aggregations["2"].buckets = aggregations;
}
return data;
}
Why? How can I try to fix this issue?
Your problem seems to be using optional chaining operator in an unsupported node version which is likely (<14
). Keep in mind that only node 14 and later can support this.
But as I know that you can use the option (--harmony
) to enable some new features but I haven't yet tested before.
In short, the solution is either to update your node version >=14
or try to use node --harmony src/index.js
.