忽略 eslint 错误:“导入”和“导出”可能只出现在顶层

问题描述 投票:0回答:5

是否可以在 eslint 中停用此错误?

Parsing error: 'import' and 'export' may only appear at the top level
lint eslint
5个回答
93
投票

ESLint 本身不支持这一点,因为这违反了规范。但是如果你使用

babel-eslint
解析器,那么在你的 eslint 配置文件中你可以这样做:

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

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


36
投票

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

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

20
投票

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

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

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

您可以在他们的在线演示中进行测试。


4
投票

就我而言:

为 React 项目添加了 javascriptreact。我想应该将项目类型添加到插件中。

{
...,
 "plugins": [
        "prettier",
        "javascriptreact"
    ],
...
}

0
投票

将其添加到您的 eslint 文件中:

env: {
  browser: true,
  commonjs: true,
  module: true,
  es2021: true,
  node: true,
 },
© www.soinside.com 2019 - 2024. All rights reserved.