How to fix unexpected token in ESLint?

你说的曾经没有我的故事 提交于 2020-12-08 06:58:38

问题


I install ESLint globally using the command npm install -g eslint on my Mac. It was successful installing, but when I run eslint -v this is the issue I encounter:

$ npm install -g eslint
path/to/bin/eslint -> path/to/lib/node_modules/eslint/bin/eslint.js
+ eslint@7.3.1
added 107 packages from 63 contributors in 4.823s

$ eslint -v
path/to/lib/node_modules/eslint/bin/eslint.js:93
        } catch {
                ^

SyntaxError: Unexpected token {
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:617:28)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)
    at startup (bootstrap_node.js:204:16)
    at bootstrap_node.js:625:3

I would like to know what are the missing steps that cause this issue? I'm using Node.js v8.16.2 and NPM v6.4.1.


回答1:


The error happens because } catch { is a relatively recent (ES2019) language feature called "optional catch binding"; prior to its introduction, binding the caught error (e.g. } catch (err) {) was required syntactically. Per node.green, you need at least Node 10 to have that language feature.

So why does this happen in ESLint? Per e.g. the release blog, version 7 has dropped support for Node 8; they're no longer testing against that version and more modern language features will be assumed to be supported.

To fix it, either:

  1. Upgrade Node (Node 8 is out of LTS, which is why ESLint dropped support); or
  2. npm install eslint@6 (with -g if you want to install globally) to use the older version of ESLint with Node 8 support.


来源:https://stackoverflow.com/questions/62636329/how-to-fix-unexpected-token-in-eslint

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!