【问题标题】:Use optional chaining operatori in Nodejs 14.x在 Nodejs 14.x 中使用可选链操作符
【发布时间】:2022-01-15 01:37:47
【问题描述】:

我尝试在 Nodejs 中使用可选链接运算符,我安装了 nvm 的窗口 10。我尝试将我的脚本与节点 14.5 和 15 一起使用,但响应始终相同

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 .

这是由 phpstorm 启动的,我尝试在 package.json 中添加此注释但不起作用

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

打包 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"
  }
}

【问题讨论】:

标签: javascript node.js optional-chaining


【解决方案1】:

package.json 中的引擎指定应支持的 Node.js 版本。见docs。 Node.js 版本 14 支持可选链接。尝试检查您当前拥有的节点版本。

node -v

【讨论】:

  • 我使用 nvm 的多个版本,但在这种特定情况下 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 运行 exec-list
  • 但是看起来你的代码是用旧的 nodejs 版本运行的。我在节点 14.17.6 上对其进行了测试,并且可选链接工作正常。
  • 是的,奇怪的是他不使用指定的那个。从 pre nvm 使用 15.6 开始并开始工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-16
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
  • 1970-01-01
  • 2020-02-16
相关资源
最近更新 更多