37
votes

Is it possible to deactivate this error in eslint?

Parsing error: 'import' and 'export' may only appear at the top level
3
This is looking like a won't fix scenario. See github.com/eslint/eslint/issues/2259 and also 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

3 Answers

59
votes

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

16
votes

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.

11
votes

my solution incase other dont work

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