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
+ 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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
user3818576
  • 2,979
  • 8
  • 37
  • 62
  • 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. – jonrsharpe Jun 29 '20 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. – user3818576 Jun 29 '20 at 10:46
  • Which version of *Node* are you using there? – jonrsharpe Jun 29 '20 at 10:46
  • nodejs v8.16.2. for npm 6.4.1. – user3818576 Jun 29 '20 at 10:48
  • 2
    ESLint 7 dropped support for Node 8: https://eslint.org/blog/2020/05/eslint-v7.0.0-released. You need that language feature, which means at least Node 10: https://node.green/#ES2019-misc-optional-catch-binding – jonrsharpe Jun 29 '20 at 10:49
  • I see. I thought it is supported by all version. thank you soo much. – user3818576 Jun 29 '20 at 10:52

2 Answers2

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.
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • That helped me, but it only works after explicitly deinstalling the outdated lint version with `npm uninstall eslint`, first. Thanks for the concise explanation! – Michael S. Aug 23 '20 at 09:49
  • 1
    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 – Ilya Kushlianski Jun 20 '21 at 17:37
  • Thanks @IlyaKushlianski. That was my problem also. – ryentzer Aug 22 '21 at 01:58
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"

cf512
  • 71
  • 4