Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I'm getting "Parsing error: Unexpected token <" from ESLint in Vue components?

I created a Laravel project and installed VSCode and all those necessary extensions. One of these is ESLint and it's working fine with JS file, but when I open a Vue component like (resources/js/component/ExampleComponent.vue), I get this error from ESLint:

"Parsing error: Unexpected token < eslint [1, 1]"

My .eslintrc.jsfile was generated using ./node_modules/.bin/eslint --init and answering the questions.

I already googled and found this solution:

npm install babel-eslint --save-dev

and add this to .eslintrc.js:

parser: "babel-eslint"

but it doesn't work anyway, the error stay there.

Here is my ExampleComponent.vue (unmodified from Laravel):

<template> (ESLint error here: "Parsing error: Unexpected token < eslint [1,1]")
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        Example Component
                    </div>

                    <div class="card-body">
                        I'm an example component.
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        mounted() {
            console.log('Component mounted.')
        }
    }
</script>

Here is my .eslintrc.js:

module.exports = {
  env: {
    browser: true,
    es6: true,
  },
  extends: 'airbnb-base',
  globals: {
    Atomics: 'readonly',
    SharedArrayBuffer: 'readonly',
  },
  parserOptions: {
    ecmaVersion: 2018,
    sourceType: 'module',
    parser: "babel-eslint",
  },
  plugins: [
    'vue',
  ],
  rules: {
  },
};
like image 862
Wevelly Felipe Avatar asked Apr 21 '19 19:04

Wevelly Felipe


People also ask

How do I fix unexpected token parsing error?

The Best Answer is. Unexpected token errors in ESLint parsing occur due to incompatibility between your development environment and ESLint's current parsing capabilities with the ongoing changes with JavaScripts ES6~7. The solution is to have ESLint parsed by a compatible parser.

How do I disable ESLint plugin vue?

If you want to disable an ESLint rule in a file or on a specific line, you can add a comment. On a single line: const message = 'foo'; console. log(message); // eslint-disable-line no-console // eslint-disable-next-line no-console console.


1 Answers

I believe you need the plugin eslint-plugin-vue, and to set your .eslintrc.js configuration to one of the bundle configurations:

  • "extends": ["plugin:vue/base"]
  • "extends": ["plugin:vue/essential"]
  • "extends": ["plugin:vue/strongly-recommended"]
  • "extends": ["plugin:vue/recommended"]

You can read more about eslint-plugin-vue here - here is a reference for a similar issue you are having.

Hope this helps!

EDIT: this is an eslint config from a vue cli project:

module.exports = {
  "root": true,
  "env": {
    "node": true
  },
  "extends": [
    "plugin:vue/essential",
    "eslint:recommended"
  ],
  "rules": {},
  "parserOptions": {
    "parser": "babel-eslint"
  }
}
like image 56
Matt Oestreich Avatar answered Nov 15 '22 03:11

Matt Oestreich