全局安装ESLint后运行eslint -v遇Unexpected Token错误的排查与修复
The Root Cause
That SyntaxError: Unexpected token { you're seeing boils down to version incompatibility between Node.js and ESLint:
- The ESLint 7.3.1 version you installed uses an ES2019 feature called parameterless catch (
catch {}), which your Node.js v8.16.2 doesn't support. Node.js didn't add support for this syntax until version 10.12.0. - When Node tries to run ESLint's code, it hits that unsupported
catch {}line and throws the syntax error immediately.
Fixes (Pick One That Fits Your Needs)
Option 1: Downgrade ESLint to a Node 8-Compatible Version
ESLint 6.x is the last major release that supports Node.js 8. Install the latest 6.x version globally with this command:
npm install -g eslint@6
Once installation finishes, running eslint -v should display the version number correctly instead of throwing an error.
Option 2: Upgrade Your Node.js Version
If your projects allow for it, upgrading Node.js to v10.12.0 or higher (preferably an LTS release like v14.x or v16.x) is the better long-term solution. It will resolve the ESLint compatibility issue and also give you access to newer Node.js features, performance improvements, and security updates.
If you use nvm (Node Version Manager) to manage multiple Node versions, here's how to switch:
# Install the latest v16 LTS release nvm install 16 # Switch to the newly installed version nvm use 16
After upgrading, reinstall ESLint globally and it should work perfectly.
内容的提问来源于stack exchange,提问作者user3818576

