ESLint Parsing error: Unexpected token
npm run lint执行eslint检查时,报:
- Parsing error: Unexpected token ……
- Parsing error: The keyword 'import' is reserved
错误。
当时的.eslintrc.js
// .eslintrc.js文件
"parserOptions": {
"parser": "babel-eslint",
}
尝试了许多方法,比如:添加sourceType
与ecmaVersion
,添加后可以解决Parsing error: The keyword 'import' is reserved
问题,但Parsing error: Unexpected token
问题依旧存在。
// .eslintrc.js文件
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2020,
"parser": "babel-eslint",
},
后来发现以下方法可以解决问题:添加'parser': '@typescript-eslint/parser'
,记得需要安装依赖npm install @typescript-eslint/parser --save-dev
。
修改后的.eslintrc.js
// .eslintrc.js文件
'parser': '@typescript-eslint/parser',
'parserOptions': {
'parser': 'babel-eslint',
},