解决 ESLint 报错 Parsing error The keyword import is reserved
解决 ESLint 在检查 ES6 模块导入时报 import is reserved 的解析器配置方案,提供 ecmaVersion 与 parserOptions 设置指南。
问题现象
在运行 npm run lint 或保存代码时,ESLint 报错:
/path/to/src/index.js
1:1 error Parsing error: The keyword 'import' is reserved
触发根因
- ESLint 默认解析器采用较为保守的 ECMAScript 5 语法规范。
- 配置文件中未声明
sourceType: "module"或未将ecmaVersion提高至现代版本。
解决步骤与操作命令
步骤一:修改 .eslintrc.js 配置文件
编辑 .eslintrc.js 或 .eslintrc.json,在 parserOptions 中放开现代模块规范:
module.exports = {
env: {
browser: true,
es2021: true,
node: true,
},
parserOptions: {
ecmaVersion: 'latest', // 启用最新 ECMAScript 语法支持
sourceType: 'module', // 关键:允许使用 import / export 模块语句
ecmaFeatures: {
jsx: true,
},
},
rules: {
// 你的业务规则
},
};
步骤二:若使用了 Babel 实验性语法,接入 @babel/eslint-parser
npm install --save-dev @babel/eslint-parser
在配置中指定解析器:
module.exports = {
parser: '@babel/eslint-parser',
parserOptions: {
requireConfigFile: false,
babelOptions: {
presets: ['@babel/preset-env'],
},
},
};
验证确认
再次运行静态代码扫描:
npx eslint src/
所有 import 语句正常解析通过,无语法解析报错。


