64

Is it possible to deactivate this error in eslint?

Parsing error: 'import' and 'export' may only appear at the top level
Paul
  • 26,170
  • 12
  • 85
  • 119
user3142695
  • 15,844
  • 47
  • 176
  • 332
  • 2
    This is looking like a won't fix scenario. See https://github.com/eslint/eslint/issues/2259 and also https://github.com/eslint/espree/issues/124 ; particularly, the quote from the dev at the end of 124, saying he basically sees no reason to support invalid syntax. – Paul Aug 26 '16 at 05:08
  • 2
    See also:[Why must import / export declarations be top level in es2015?](http://stackoverflow.com/questions/34203325/why-must-export-import-declarations-be-on-top-level-in-es2015) – Paul Aug 26 '16 at 05:11

4 Answers4

89

ESLint natively doesnt support this because this is against the spec. But if you use babel-eslint parser then inside your eslint config file you can do this:

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

Doc ref: https://github.com/babel/babel-eslint#configuration

Gyandeep
  • 12,726
  • 4
  • 31
  • 42
33

my solution incase other dont work

"parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module"
}
mayo19
  • 647
  • 8
  • 16
20

Support for dynamic import has been added in eslint 6.2.

You need to set ecmaVersion to 11 (or 2020) though.

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

You can test that in their online demo.

Luizgrs
  • 4,765
  • 1
  • 22
  • 28
4

In my case:

Added javascriptreact for a react project. I guess the project type should be added to the plugins.

{
...,
 "plugins": [
        "prettier",
        "javascriptreact"
    ],
...
}
ouflak
  • 2,458
  • 10
  • 44
  • 49