Steve
Steve

Reputation: 4543

Parsing error: Unexpected token prettier/prettier caused by "<!DOCTYPE html>"

I have this vue app which I create using vue cli and the version I use is vue2 (with eslint and prettier).

I could run npm run serve and load my page. But in Visual Studio Code, I notice this error:

{
    "resource": "/c:/vue/app2/public/index.html",
    "owner": "eslint",
    "code": {
        "value": "prettier/prettier",
        "target": {
            "$mid": 1,
            "external": "https://github.com/prettier/eslint-plugin-prettier#options",
            "path": "/prettier/eslint-plugin-prettier",
            "scheme": "https",
            "authority": "github.com",
            "fragment": "options"
        }
    },
    "severity": 4,
    "message": "Parsing error: Unexpected token",
    "source": "eslint",
    "startLineNumber": 1,
    "startColumn": 2,
    "endLineNumber": 1,
    "endColumn": 2
}

and this is my .eslintrc.js which is auto generated when I create the app and I didn't make any changes to it since then.

module.exports = {  
  root: true,
  env: {
    node: true
  },
  extends: ["plugin:vue/essential", "eslint:recommended", "@vue/prettier"],
  parserOptions: {
    ecmaVersion: 2020
  },
  rules: {
    "no-console": process.env.NODE_ENV === "production" ? "warn" : "off",
    "no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off"
  }
};

I notice that the error is actually referring to this line instead. Anyone knows what's wrong with this?

<!DOCTYPE html>

Upvotes: 22

Views: 24969

Answers (3)

Mehdi Benhamouche
Mehdi Benhamouche

Reputation: 99

Yes you need to specify parser for both 'basic' html and the .component.html (for angular components)

{
  "tabWidth": 2,
  "useTabs": false,
  "singleQuote": true,
  "semi": true,
  "bracketSpacing": true,
  "arrowParens": "avoid",
  "trailingComma": "es5",
  "bracketSameLine": true,
  "printWidth": 80,
  "endOfLine": "auto",
  "overrides": [
    {
      "files": "*.html",
      "options": {
        "parser": "html"
      }
    },
    {
      "files": "*.component.html",
      "options": {
        "parser": "angular"
      }
    }
  ]
}

Upvotes: 2

George Wayne
George Wayne

Reputation: 191

I've met a similar problem. It was caused by in vue3.0 app.

we need a eslint plugin : eslint-plugin-html, add the plugin to your ESLint configuration.

plugins: ['html'],

and you want linting HMTL, use other plugins like @eslint-html

Upvotes: 2

satanTime
satanTime

Reputation: 13574

I've met a similar issue. It was caused by <!DOCTYPE html>.

The fix is quite easy, we need to specify a parser in prettierrc, although it is obvious:

overrides:
  - files: '*.html'
    options:
      parser: 'html'
  - files: '*.component.html'
    options:
      parser: 'angular'

After that prettier is able to format files with <!DOCTYPE html>.

Creds go to krave1986

Upvotes: 36

Related Questions