Sonar can't scan Vue.js files "import' and 'export' may appear only with 'sourceType: module'"

INFO: SonarScanner 4.5.0.2216
INFO: Java 11.0.3 AdoptOpenJDK (64-bit)
INFO: Mac OS X 10.15.6 x86_64

While scanning .vue files, sonar-scanner runs into the following error for Vue.js files:

ERROR: Failed to parse file [resources/js/component.vue] at line 203: 'import' and 'export' may appear only with 'sourceType: module'

It seems like it’s not understanding the Babel config correctly or something. I’ve got sourceType set correctly in the .eslintrc.js config as well:

    "parserOptions": {
        "parser": "@babel/eslint-parser",
        "ecmaVersion": 2020,
        "sourceType": "module",
    },

Any ideas how I should debug this further? It’s not clear why this error is popping up considering that running ESLint independently works fine

Hello @fabis94,

SonarJS does not consider any .eslintrc.js files that might come along your project, but rather uses its own parsing options. We are actually thinking of making this parameterizable.

I am a bit surprised to see the error you’re facing since our analyser tries to first parse Vue files with sourceType set to module, and falls back to script in case it fails. The latter shouldn’t work because import statements are only relevant for modules.

Would it be possible to share the full logs of the scanner ? You can get them by running a scan with -X option. Also, a small reproducer to share could definitely help.

Yassin

1 Like