You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular升级后ESLint报‘export’关键字保留错误如何解决?

解决ESLint报错"The keyword 'export' is reserved"的方法

1. 配置ESLint识别ES模块语法

在项目的ESLint配置文件(比如.eslintrc.json、.eslintrc.js)里,调整解析器选项和环境设置,让ESLint能识别ES6+的模块语法:

  • 在parserOptions里添加"sourceType": "module"
  • 确保env字段包含"es6": true

示例配置(.eslintrc.json):

{
  "parserOptions": {
    "ecmaVersion": "latest",
    "sourceType": "module"
  },
  "env": {
    "es6": true,
    "browser": true
  }
}

2. 检查文件扩展名

确认这个纯JS库的文件后缀是.js——如果是.cjs(CommonJS模块),ESLint会默认按CommonJS规则解析,用export就会触发报错。如果是ES模块,保持.js后缀即可;如果是CommonJS模块,把export function...改成module.exports = calculateTotalCosts。

3. 给特定文件单独配置规则

不想全局改配置的话,可以直接在这个JS库文件顶部加ESLint注释:

/* eslint-env es6 */
/* eslint parserOptions: { sourceType: "module" } */

或者在ESLint配置的overrides字段里单独指定该文件的规则:

{
  "overrides": [
    {
      "files": ["src/path/to/your-library.js"],
      "parserOptions": {
        "sourceType": "module"
      },
      "env": {
        "es6": true
      }
    }
  ]
}

4. 确认Angular ESLint插件版本匹配

因为是Angular项目,你用的应该是@angular-eslint插件,要确保插件版本和你的Angular版本对应。版本不兼容也可能导致解析错误,重新安装对应版本的插件试试:

npm install @angular-eslint/eslint-plugin@匹配你的Angular版本号 --save-dev

内容的提问来源于stack exchange,提问作者Tom O'Brien

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 07:42:08