"Parsing error: Unexpected token < ..." using VueCli3 and airbnb eslint
Asked Answered
D

3

6

I know this may seem like a duplicate question, I still get this type of eslint errors after trying the following:

Here is part of my package.json setting:

"devDependencies": {
    "@vue/cli-plugin-babel": "^3.0.5",
    "@vue/cli-plugin-eslint": "^3.0.5",
    "@vue/cli-plugin-unit-jest": "^3.0.5",
    "@vue/cli-service": "^3.0.5",
    "@vue/eslint-config-airbnb": "^4.0.0",
    "@vue/test-utils": "^1.0.0-beta.20",
    "babel-core": "7.0.0-bridge.0",
    "babel-eslint": "^10.0.1",
    "babel-jest": "^23.6.0",
    "eslint": "^5.8.0",
    "eslint-config-airbnb-base": "^13.1.0",
    "eslint-plugin-import": "^2.14.0",
    "eslint-plugin-vue": "^5.0.0-0",
    "vue-template-compiler": "^2.5.17"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "@vue/airbnb"
    ],
    "rules": {},
    "parserOptions": {
      "parser": "babel-eslint",
      "ecmaVersion": 6
    }
  },

Here is my .eslintrc.js file:

module.exports = {
  parserOptions: {
    sourceType: 'module',
    allowImportExportEverywhere: true,
  },
  rules: {
    'no-console': 0,
  },
  plugins: ['vue'],
};

Here is a detailed version of eslint error:

error: Parsing error: Unexpected token < at src/App.vue:1:1:
> 1 | <template>
    | ^
  2 |   <div id="app">
  3 |     <router-view/>
  4 |   </div>

Here is my App.vue source code:

<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script>
export default {
  name: 'BuilderApp',
  data() {
    return {
      dragEvents: ['drag', 'dragstart', 'dragend', 'dragover', 'dragenter', 'dragleave', 'drop'],
    };
  },
  mounted() {
    // prevent default action for all defined dragEvents
    if (this.dragAndDropCapable()) {
      this.dragEvents.forEach((dragEvent) => {
        document.getElementById('app').addEventListener(dragEvent, (e) => {
          e.preventDefault();
          e.stopPropagation();
        }, false);
      });
    }
  },
  methods: {
    dragAndDropCapable() {
      const testDiv = document.createElement('div');
      return (('draggable' in testDiv)
          || ('ondragstart' in testDiv && 'ondrop' in testDiv))
          && 'FormData' in window
          && 'FileReader' in window;
    },
  },
};
</script>

<style>
.filter-disable .filter-count {
  background: #dadada;
}
</style>

I'm not sure what I am missing.

Dorthydortmund answered 3/12, 2018 at 20:43 Comment(1)
Have you tried merging the eslint config at package.json into .eslintrc.js? The latter probably has more priority, so some configs (like the extends one) aren't applied.Odysseus
D
11

You miss the "parser" in your .eslintrc file.

Here is my .eslintrc

{
  "parser": "vue-eslint-parser",
  "parserOptions": {
    "ecmaVersion": 7,
    "sourceType": "module"
  }
}

vue-eslint-parser doc

Dorseydorsiferous answered 21/4, 2019 at 3:10 Comment(1)
In my case I had to switch the ecmaVersion from 7 to 2018Wizardly
E
0

I had the same problem and was fixed by adding the .eslintrc.json file with something like:

{
    "rules": {
        "no-console": 0,
        "quotes": [2, "double", "avoid-escape"]
    },
    "parser":"vue-eslint-parser",
    "parserOptions": {
        "ecmaVersion": 7,
        "sourceType": "module",
        "ecmaFeatures": {
            "jsx": true,
            "modules": true
        }
    }
}

And then I had to install the eslint-plugin-vue following this.

I found this solution in here and here.

And I forgot to mention that I also follow this one, after all the changes.

Hope this helps is pretty basic, but I didn't know that.

Enterprising answered 4/2, 2020 at 22:50 Comment(0)
I
0

Change your methods: {...} to methods: function(){...}

I had a similar problem with:

created: {
    url_parts = window.location.href.split("/")
    slug = url_parts[3]
    url = `/api/inventory/product/${slug}`

    axios
      .get(url, slug)
      .then(...)
}

I was getting the following error:

Failed to compile.

./src/components/StoreInfo.vue
Module Error (from ./node_modules/eslint-loader/index.js):

/Users/alkadelik/Documents/Dev/Django/div/cart_cli/src/components/StoreInfo.vue
  30:4  error  Parsing error: Unexpected token, expected ","

  16 | 
  17 |     url_parts = window.location.href.split("/")
> 18 |     slug = url_parts[3]
     |     ^
  19 |     url = ""

The error went away when I converted to:

created: function(){
    const axios = require('axios'); // had to introduce this

    // also notice the introduction of 'let'
    let url_parts = window.location.href.split("/")
    let slug = url_parts[3]
    let url = `/api/inventory/product/${slug}`

    axios
      .get(url, slug)
      .then(...)
}
Incardination answered 4/5, 2020 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.