Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix unexpected token in ESLint?

Tags:

npm

eslint

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.

like image 250
user3818576 Avatar asked Jun 29 '20 10:06

user3818576


People also ask

How do I fix an unexpected token?

This error can occur for a number of reasons, but is typically caused by a typo or incorrect code. Luckily, the SyntaxError: Unexpected token error is relatively easy to fix. In most cases, the error can be resolved by checking the code for accuracy and correcting any mistakes.

What does parsing error unexpected token mean?

The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.

What is a parsing error?

A parse error is an error message you sometimes get on Android devices when an app fails to install. The message itself is not very specific, and there are a lot of problems that can cause it.


1 Answers

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.
like image 169
jonrsharpe Avatar answered Oct 26 '22 11:10

jonrsharpe