2

i try to use optional chaining operator in Nodejs, i have window 10 with nvm installed. I have try use my script with node 14.5 and 15 but the response it's always the same

C:\Users\user1\AppData\Roaming\nvm\v15.6.0\node64.exe C:\Users\user1\AppData\Roaming\nvm\v15.6.0\node_modules\npm\bin\npm-cli.js run exec-list --scripts-prepend-node-path=auto

> oracle-node-service@1.0.0 exec-list   
> node src/exec.js list json=myfile.json

C:\xampp\htdocs\oracle-node-service\src\exec.js:388
    ...setup.templates.handlebars?.filters?.FILTERS[0].COLUMNS_UNIQUE,      
                                  ^

SyntaxError: Unexpected token .

this is started by phpstorm, i have tryed add this annotation in package.json but not work

  "engines": {
    "node": "14.x"
  }
}

PACKACGE JSON

{
  "name": "oracle-node-service",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "build": "babel ./src --out-dir ./build",
    "start": "node src/index.js",
    "start-dev": "nodemon --exec babel-node src/index.js",
    "lint": "eslint .",
    "test": "echo \"Error: no test specified\" && exit 1",
    "exec": "node src/exec.js list detail filters json=myfile.json",
    "exec-list": "node src/exec.js list json=myfile.json",
    "exec-detail": "node src/exec.js detail json=myfile.json",
    "exec-filters": "node src/exec.js list detail filters json=configuration-filters.json",
    "exec-list-detail": "node src/exec.js list detail json=configuration-list-detail.json",
    "exec-filters-list-detail": "node src/exec.js filters list detail json=configuration-filters-list-detail.json"
  },
  "license": "ISC",
  "devDependencies": {
    "@babel/cli": "^7.14.5",
    "@babel/core": "^7.14.6",
    "@babel/node": "^7.14.5",
    "@babel/preset-env": "^7.14.5",
    "@babel/runtime": "^7.14.6",
    "eslint": "^7.29.0",
    "eslint-config-airbnb-base": "^14.2.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-import": "^2.23.4",
    "eslint-plugin-prettier": "^3.4.0",
    "nodemon": "^2.0.7"
  },
  "dependencies": {
    "handlebars": "^4.7.7",
    "moment": "^2.29.1",
    "oracledb": "^5.2.0",
    "prompt-sync": "^4.2.0",
    "lodash": "^4.17.21"
  },
  "engines": {
    "node": "14.x"
  }
}
Alex Gor
  • 533
  • 4
  • 18
Luca Bottoni
  • 81
  • 1
  • 9
  • 1
    `?.` is optional chaining, not nullish coalescing. – jabaa Dec 10 '21 at 10:41
  • 2
    That is [Optional chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) not the [Nullish coalescing operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator) – Reyno Dec 10 '21 at 10:41

1 Answers1

1

Engines in package.json specify what version of Node.js should be supported. See docs. Optional chaining supported from Node.js version 14. Try to check what node version you have currently.

node -v
Alex Gor
  • 533
  • 4
  • 18
  • i use multiple version with nvm, but in this specific case C:\Users\user1\AppData\Roaming\nvm\v15.6.0\node64.exe C:\Users\user1\AppData\Roaming\nvm\v15.6.0\node_modules\npm\bin\npm-cli.js run exec-list – Luca Bottoni Dec 10 '21 at 11:33
  • 1
    But it's looks like you code run with older nodejs version. I tested it on node 14.17.6 and optional chaining working properly. – Alex Gor Dec 10 '21 at 11:56
  • yes, strange that he does not use the one indicated. started with pre nvm use 15.6 and work – Luca Bottoni Dec 10 '21 at 14:37