【问题标题】:ignore eslint error: 'import' and 'export' may only appear at the top level忽略 eslint 错误:'import' 和 'export' 可能只出现在顶层
【发布时间】:2021-12-14 00:45:33
【问题描述】:

eslint 中是否可以解除这个错误?

Parsing error: 'import' and 'export' may only appear at the top level

【问题讨论】:

标签: lint eslint


【解决方案1】:

ESLint 本身不支持这一点,因为这违反了规范。但是如果你使用 babel-eslint 解析器,那么在你的 eslint 配置文件中你可以这样做:

{
  "parser": "babel-eslint",
  "parserOptions": {
    "sourceType": "module",
    "allowImportExportEverywhere": true
  }
}

文档参考:https://github.com/babel/babel-eslint#configuration

【讨论】:

  • 似乎不需要 babel-eslint
  • 非顶级导入现在是规范的一部分并被广泛使用
  • 现在支持esline check my answer
【解决方案2】:

eslint 6.2 中添加了对动态导入的支持。

您需要将 ecmaVersion 设置为 11(或 2020)。

"parserOptions": {
    "ecmaVersion": 11
    ...
}

您可以在他们的online demo 中进行测试。

【讨论】:

  • 会这样工作吗? "eslintConfig": { "extends": "react-app", "parserOptions": { "ecmaVersion": 11 } },
  • 仅此一项对我不起作用,我也必须添加以下行 - "sourceType": "module"
  • 它在sourceType: script的在线演示中确实有效
【解决方案3】:

我的解决方案以防其他人不起作用

"parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module"
}

【讨论】:

    猜你喜欢
    • 2021-08-08
    • 2020-06-05
    • 2021-02-02
    • 2017-05-07
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 2018-12-04
    相关资源
    最近更新 更多