Eslint-plugin-vue: Parsing error: 'import' and 'export' may only appear at the top level

Created on 27 Aug 2018  路  6Comments  路  Source: vuejs/eslint-plugin-vue

Tell us about your environment

  • ESLint Version: 5.4.0
  • eslint-plugin-vue Version: 5.0.0-beta.3
  • Node Version: 10.8

Please show your full configuration:

module.exports = {
  root: true,
  extends: [
    'plugin:vue/recommended',
    'dwing'
  ],
  parserOptions: {
    sourceType: 'module',
    allowImportExportEverywhere: true
  }
};

What did you do? Please include the actual source code causing the issue.

export default {
  name: 'Aplayer',
  mounted() {
    const that = this;
    // error
    import('aplayer').then(({ default: Aplayer }) => {

    });
  }
};

What did you expect to happen?

dynamic import for performance

What actually happened? Please include the actual, raw output from ESLint.

{
    "resource": "/Users/willin/Documents/websites/blog/src/.vuepress/theme/components/sidebar/player.vue",
    "owner": "eslint",
    "code": "undefined",
    "severity": 8,
    "message": "Parsing error: 'import' and 'export' may only appear at the top level",
    "source": "eslint",
    "startLineNumber": 12,
    "startColumn": 4,
    "endLineNumber": 12,
    "endColumn": 4
}
question

Most helpful comment

Dynamic import may not be supported by eslint default parser.
It seems to solve by using babel-eslint.

https://github.com/eslint/eslint/issues/9927

Could you try it?

module.exports = {
  root: true,
  extends: [
    'plugin:vue/recommended',
    'dwing'
  ],
  parserOptions: {
+    parser: "babel-eslint",
    sourceType: 'module',
    allowImportExportEverywhere: true
  }
};

All 6 comments

Dynamic import may not be supported by eslint default parser.
It seems to solve by using babel-eslint.

https://github.com/eslint/eslint/issues/9927

Could you try it?

module.exports = {
  root: true,
  extends: [
    'plugin:vue/recommended',
    'dwing'
  ],
  parserOptions: {
+    parser: "babel-eslint",
    sourceType: 'module',
    allowImportExportEverywhere: true
  }
};

works

but i think it may be built in...

Vuepress recommends dynamic imports for certain non-SSR friendly situations. Is there a way to disable this rule by default?

This is not a rule. It happens in the parsing phase and ESLint doesn't support Stage 3 features by default.

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

parser should go before parserOptions, not inside it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

soullivaneuh picture soullivaneuh  路  3Comments

apertureless picture apertureless  路  4Comments

sqal picture sqal  路  3Comments

mitar picture mitar  路  3Comments

maple-leaf picture maple-leaf  路  3Comments