简体   繁体   English

VSCode ESLint 如何修复解析错误:'import' 和 'export' 可能只出现在顶层

[英]VSCode ESLint howto fix parsing error: 'import' and 'export' may only appear at the top level

NOTE: The Code is working, only ESLint is giving me an error which i want to fix!!注意:代码正在运行,只有 ESLint 给了我一个我想修复的错误!!

When doing a dynamic import:进行动态导入时:

if (true) import x from 'y'; //Parsing error: 'import' and 'export' may only appear at the top level

my .eslintrc我的.eslintrc

{
  "root": true,
  "extends": "eslint:recommended",
  "env": {
    "es6": true,
    "node": true,
    "browser": true
  },
  "parserOptions": {
    "ecmaVersion": 10,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "rules": {
    "strict": 0,
    "no-undef": 2,
    "accessor-pairs": 2,
    "comma-dangle": [2, "always-multiline"],
    "consistent-return": 2,
    "dot-notation": 2,
    "eqeqeq": 2,
    "indent": [2, 2, {"SwitchCase": 1}],
    "no-cond-assign": 2,
    "no-constant-condition": 2,
    "no-eval": 2,
    "no-inner-declarations": [0],
    "no-unneeded-ternary": 2,
    "radix": 2,
    "semi": [2, "always"],
    "camelcase": 2,
    "comma-spacing": 2,
    "comma-style": 2,
    "eol-last": 2,
    "linebreak-style": [2, "unix"],
    "new-parens": 2,
    "no-lonely-if": 2,
    "no-multiple-empty-lines": 2,
    "no-nested-ternary": 2,
    "no-spaced-func": 2,
    "no-trailing-spaces": 2,
    "operator-linebreak": 2,
    "quotes": [2, "single"],
    "semi-spacing": 2,
    "space-unary-ops": 2,
    "arrow-parens": 2,
    "arrow-spacing": 2,
    "no-class-assign": 2,
    "no-dupe-class-members": 2,
    "no-var": 2,
    "object-shorthand": 2,
    "prefer-const": 2,
    "prefer-spread": 2,
    "prefer-template": 2
  },

i already tried:我已经尝试过:
...switching ecmaVersion to, 11, 2018, 2019, 2020 which give me either wrong number or parser didn't work anymore ...将ecmaVersion切换到 11, 2018, 2019, 2020 这给我错误的数字或解析器不再工作
...added parser: babel-eslint which make the parser not work anymore ...添加parser: babel-eslint使解析器不再工作
...added allowImportsExportsAnywhere: true which did nothing ...添加了allowImportsExportsAnywhere: true什么也没做

See https://eslint.org/blog/2019/08/eslint-v6.2.0-released#highlightshttps://eslint.org/blog/2019/08/eslint-v6.2.0-released#highlights

This release adds support for ES2020 syntax, which includes support for Dynamic Imports and BigInt.此版本增加了对 ES2020 语法的支持,其中包括对动态导入和 BigInt 的支持。 This can be enabled using ecmaVersion: 2020 in your configuration file.这可以在您的配置文件中使用ecmaVersion: 2020启用。

That would seem to suggest adding this to your .eslintrc.json file and reloading vscode:这似乎建议将此添加到您的.eslintrc.json文件并重新加载 vscode:

"parserOptions": {
  "ecmaVersion": 2020,
  "sourceType": "module",
  "ecmaFeatures": {
    "jsx": true
  }
},

or "ecmaVersion": 11, which is the same thing."ecmaVersion": 11,这是同一件事。

But I get a different error option about an invalid ecmaVersion when I do that.但是当我这样做时,我得到了一个关于无效ecmaVersion的不同错误选项。 This seems to remove that error :这似乎消除了该错误

"env": {
    "browser": true,
    "node": true,
    "es2020": true
},

and no ecmaVersion in the parserOptions !并且ecmaVersion中没有parserOptions Apparently vscode's version of eslint doesn't support "ecmaVersion": 2020 yet.显然 vscode 的 eslint 版本还不支持"ecmaVersion": 2020

Maybe you can use in this structure也许你可以在这个结构中使用

const x =
  your condition ? require("y") : () => null;

In your component在您的组件中

{ your condition ?(<x/>):("")}

Looks like the syntax is wrong.貌似语法不对。 Try this:尝试这个:

if (true) 
  import('y').then((x) => {
     console.log(x);
  });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Svelte - “'import' 和 'export' 可能只出现在顶层”错误 - Svelte - "'import' and 'export' may only appear at the top level" error create-react-app eslint 错误“解析错误:‘import’和‘export’可能只出现在‘sourceType:module’中” - create-react-app eslint error "Parsing error: 'import' and 'export' may appear only with 'sourceType: module'" 错误:当我将一小部分 REACT 代码修改为 REDUX 时,“导入”和“导出”可能仅出现在顶层 - Error: 'import' and 'export' may only appear at the top level when I reworked small part of REACT code to REDUX 语法错误:导入声明只能出现在模块的顶层 - Syntax error: import declarations may only appear at top level of a module 编译失败。 ./src/reportWebVitals.js 中的错误。 reportWebVitals.js:“导入”和“导出”可能只出现在顶层 (3:4) - Failed to compile. Error in ./src/reportWebVitals.js. reportWebVitals.js: 'import' and 'export' may only appear at the top level (3:4) ReactJS &#39;import&#39; 和 &#39;export&#39; 可能只出现在顶层有什么问题? - ReactJS What's wrong 'import' and 'export' may only appear at the top level? 未捕获的语法错误:导出声明可能只出现在模块的顶层 - Uncaught SyntaxError: export declarations may only appear at top level of a module 导入声明可能只出现在模块的顶层,尝试 type=&quot;module&quot; 只会给我一个新错误 - Import declarations may only appear at top level of a module and trying type=“module” just gives me a new error 升级 Gulp 后,javascript 抛出语法错误“导入声明可能只出现在模块的顶层” - After upgrading Gulp, javascript is throwing an syntax-error “import declarations may only appear at top level of a module” 如何告诉 eslint:禁用下一行“&#39;import&#39;和&#39;export&#39;可能只与&#39;sourceType:module&#39;一起出现” - How to tell eslint to: disable next line "'import' and 'export' may appear only with 'sourceType: module'"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM