14

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
+ [email protected]
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.

6
  • You're either not using Babel or don't have the setting (optional catch binding) that allows you to skip the error "param" - } catch (e) { will work. Commented Jun 29, 2020 at 10:41
  • I'm installing it globally. Do I need to set something inside global eslint folder? I dont get it why I need to add param. I just want to show my eslint version. Commented Jun 29, 2020 at 10:46
  • Which version of Node are you using there? Commented Jun 29, 2020 at 10:46
  • nodejs v8.16.2. for npm 6.4.1. Commented Jun 29, 2020 at 10:48
  • 2
    ESLint 7 dropped support for Node 8: eslint.org/blog/2020/05/eslint-v7.0.0-released. You need that language feature, which means at least Node 10: node.green/#ES2019-misc-optional-catch-binding Commented Jun 29, 2020 at 10:49

2 Answers 2

18

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.
Sign up to request clarification or add additional context in comments.

2 Comments

That helped me, but it only works after explicitly deinstalling the outdated lint version with npm uninstall eslint, first. Thanks for the concise explanation!
My Webstorm was pointing to node 8 for some reason, although I have 14 installed. Had to manually point the IDE to node 14, and the error obviously went away
0

In case it's helpful for anyone, I had a slightly different twist on the other answer. In my case, the error was happening during the Travis CI build process and causing it to fail. The solution in my case was to update my .travis.yml file to node_js: "16"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.