313

With this code:

import React from 'react';
import { Link } from 'react-router';
import { View, NavBar } from 'amazeui-touch';

import * as Pages from '../components';

const {  Home, ...Components } = Pages;

I get this eslint error:

7:16  error  Parsing error: Unexpected token .. Why?

Here is my eslint config:

{
  "extends": "airbnb",
  "rules": {
    /* JSX */
    "react/prop-types": [1, {
      "ignore": ["className", "children", "location", "params", "location*"]
    }],
    "no-param-reassign": [0, {
      "props": false
    }],
    "prefer-rest-params": 1,
    "arrow-body-style": 0,
    "prefer-template": 0,
    "react/prefer-stateless-function": 1,
    "react/jsx-no-bind": [0, {
      "ignoreRefs": false,
      "allowArrowFunctions": false,
      "allowBind": true
    }],
  }
}

.... .... What's the problem?

Brigand
  • 84,529
  • 20
  • 165
  • 173
DongYao
  • 3,171
  • 2
  • 10
  • 4

17 Answers17

426

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.

Adding the "parserOptions" property to your .eslintrc is no longer enough for particular situations, such as using

static contextTypes = { ... } /* react */

in ES6 classes as ESLint is currently unable to parse it on its own. This particular situation will throw an error of:

error Parsing error: Unexpected token =

The solution is to have ESLint parsed by a compatible parser, i.e @babel/eslint-parser or babel-eslint for babel version below v7.

just add:

"parser": "@babel/eslint-parser"

to your .eslintrc file and run npm install @babel/eslint-parser --save-dev or yarn add -D @babel/eslint-parser.

Please note that as the new Context API starting from React ^16.3 has some important changes, please refer to the official guide.

hanorine
  • 7,256
  • 3
  • 14
  • 18
  • 11
    `yarn add -D babel-eslint` for those using Yarn. – Neurotransmitter Sep 13 '18 at 16:53
  • 24
    For those who don't know where to add the `"parser": "babel-eslint"` config statement, it's in the `.eslintrc.json`. In my case, it's a JSON file, but basically, your .eslintrc file – Avid Programmer Jan 24 '19 at 21:57
  • 3
    Note * If you have "ejected" your create-react-app and you are adding eslint-ing to your IDE, babel-eslint is already applied. Just add the parser and your good to go. – Wes Duff Feb 19 '19 at 18:05
  • 1
    I found this article helpful too: https://grantnorwood.com/eslint-parsing-error-unexpected-token-visual-studio-code/ – gurun8 Nov 16 '19 at 13:40
  • I added the `"parser": "babel-eslint"` to my eslintrc file but got multiple errors instead. I am working with react and it tells me that My imported components were called but never used. Which is wierd. – David Essien Apr 10 '20 at 07:47
  • If using "parser": "babel-eslint", then showing other error like { } for destructing. – Pankaj May 05 '20 at 09:50
  • Pankaj - restructuring requires a babel plugin - such as `@babel/preset-env`. – hanorine May 06 '20 at 14:06
  • Even after adding this, a pre-requisite is that node version should be 8 or above. – Shabbir Essaji Dec 19 '20 at 09:45
  • 7
    `babel-eslint` is outdated, use `@babel/eslint-parser` – jackblk Jul 27 '21 at 08:49
  • 1
    solution works with NestJS too, just remove the quotes from "parser" and add it to your .eslintrc.js and then install babel. - instead "parser": "@babel/eslint-parser" do parser: "@babel/eslint-parser". – LaCodeM Oct 22 '21 at 15:16
  • add `parser` where in the eslintrc file? – john k Jun 10 '22 at 18:39
107

In my case (im using Firebase Cloud Functions) i opened .eslintrc.json and changed:

"parserOptions": {
  // Required for certain syntax usages
  "ecmaVersion": 2017
},

to:

"parserOptions": {
  // Required for certain syntax usages
  "ecmaVersion": 2020
},
Dmitry Grinko
  • 13,806
  • 14
  • 62
  • 86
Alvin Konda
  • 2,898
  • 1
  • 22
  • 22
  • 1
    Changing `emcaVersion` to `2020` did the trick for me when receiving this error for using `var?.prop` in a React app. Thank you for posting. – Jason R Stevens CFA Dec 29 '20 at 02:26
  • 1
    changing `ecmaVersion: 2017` to `ecmaVersion: 2020` returned an error: `error Parsing error: Invalid ecmaVersion`. What else should I do besides changing to 2020? – Aliton Oliveira Apr 29 '21 at 00:54
  • 8
    Instead of changing `ecmaVersion: 2017` to `ecmaVersion: 2020` I just changed `"scripts": { "lint": "eslint ." }` to `"scripts": { "lint": "eslint" }` in the file **package.json** . – Aliton Oliveira Apr 29 '21 at 01:03
88

"parser": "babel-eslint" helped me to fix the issue

{
    "parser": "babel-eslint",
    "parserOptions": {
        "ecmaVersion": 6,
        "sourceType": "module",
        "ecmaFeatures": {
            "jsx": true,
            "modules": true,
            "experimentalObjectRestSpread": true
        }
    },
    "plugins": [
        "react"
    ],
    "extends": ["eslint:recommended", "plugin:react/recommended"],
    "rules": {
        "comma-dangle": 0,
        "react/jsx-uses-vars": 1,
        "react/display-name": 1,
        "no-unused-vars": "warn",
        "no-console": 1,
        "no-unexpected-multiline": "warn"
    },
    "settings": {
        "react": {
            "pragma": "React",
            "version": "15.6.1"
        }
    }
}

Reference

Rajendran Nadar
  • 4,962
  • 3
  • 30
  • 51
84

ESLint 2.x experimentally supports ObjectRestSpread syntax, you can enable it by adding the following to your .eslintrc: docs

"parserOptions": {
  "ecmaVersion": 6,
  "ecmaFeatures": {
    "experimentalObjectRestSpread": true
  }
},

ESLint 1.x doesn't natively support the spread operator, one way to get around this is using the babel-eslint parser. The latest installation and usage instructions are on the project readme.

Kevan Ahlquist
  • 5,375
  • 1
  • 17
  • 25
35

I solved this issue by First, installing babel-eslint using npm

npm install babel-eslint --save-dev

Secondly, add this configuration in .eslintrc file

{
   "parser":"babel-eslint"
}
Joee
  • 1,834
  • 18
  • 19
  • 4
    However [babel-eslint](https://www.npmjs.com/package/babel-eslint) is no longer supported. Use its successor, [@babel/eslint-parser](https://www.npmjs.com/package/@babel/eslint-parser). `npm i -D @babel/core @babel/eslint-parser` then `{"parser: "@babel/eslint-parser"}` – Matt Norris Apr 09 '21 at 14:58
16

Originally, the solution was to provide the following config as object destructuring used to be an experimental feature and not supported by default:

{
  "parserOptions": {
    "ecmaFeatures": {
      "experimentalObjectRestSpread": true
    }
  }
}

Since version 5, this option has been deprecated.

Now it is enough just to declare a version of ES, which is new enough:

{
  "parserOptions": {
    "ecmaVersion": 2018
  }
}
Vojtech Ruzicka
  • 16,384
  • 15
  • 63
  • 66
15

I'm using eslint for cloud-functions (development env: flutter 2.2.3).

In my case .eslintrc.json does not exist so I had to update the .eslintrc.js file by including parserOptions: { "ecmaVersion": 2020, }, property at the end of file. My updated .eslintrc.js file looks like this:

module.exports = {
  root: true,
  env: {
    es6: true,
    node: true,
  },
  extends: [
    "eslint:recommended",
    "google",
  ],
  rules: {
    quotes: ["error", "double"],
  },
  
  // Newly added property
  parserOptions: {
    "ecmaVersion": 2020,
  },
};
sh_ark
  • 557
  • 5
  • 14
  • Works perfect. [Also check browser support](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) – Shanimal Feb 01 '22 at 17:35
12

I solved this problem by setting this in .eslintrc.json file:

"extends": [
    ...,
    "plugin:prettier/recommended"
]
Dharman
  • 30,962
  • 25
  • 85
  • 135
Pazu
  • 171
  • 3
  • 8
10

Just for the record, if you are using eslint-plugin-vue, the correct place to add 'parser': 'babel-eslint' is inside parserOptions param.

  'parserOptions': {
    'parser': 'babel-eslint',
    'ecmaVersion': 2018,
    'sourceType': 'module'
  }

https://eslint.vuejs.org/user-guide/#faq

Cristiano
  • 134
  • 2
  • 4
8

In febrary 2021 you can use theese values

ecmaVersion - set to 3, 5 (default), 6, 7, 8, 9, 10, 11, or 12 to specify the version of ECMAScript syntax you want to use. You can also set to 2015 (same as 6), 2016 (same as 7), 2017 (same as 8), 2018 (same as 9), 2019 (same as 10), 2020 (same as 11), or 2021 (same as 12) to use the year-based naming.

https://eslint.org/docs/user-guide/configuring/language-options#specifying-parser-options

5

For React + Firebase Functions

Go to : functions -> .eslintrc.js

Add it - parserOptions: { ecmaVersion: 8, },

module.exports = {
  root: true,
  env: {
    es6: true,
    node: true,
  },
  parserOptions: {
    ecmaVersion: 8,
  },
  extends: ["eslint:recommended", "google"],
  rules: {
    quotes: ["error", "double"],
  },
};
Pankaj Kumar
  • 422
  • 5
  • 10
3

In my case adding "parser": "@typescript-eslint/parser", line into my .eslintrc file helped:

  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": ["tsconfig.json"],
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true,
      "modules": true,
      "experimentalObjectRestSpread": true
    }
  },

package.json in the same time has these 2 lines:

    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
Jack Pts
  • 119
  • 1
  • 3
1

If you have got a pre-commit task with husky running eslint, please continue reading. I tried most of the answers about parserOptions and parser values where my actual issue was about the node version I was using.

My current node version was 12.0.0, but husky was using my nvm default version somehow (even though I didn't have nvm in my system). This seems to be an issue with husky itself. So:

  1. I deleted $HOME/.nvm folder which was not deleted when I removed nvm earlier.
  2. Verified node is the latest and did proper parser options.
  3. It started working!
Asim K T
  • 16,864
  • 10
  • 77
  • 99
1

I was facing the issue despite implementing all the above solutions. When I downgraded the eslint version, it started working

  • 9
    Your answer could have been helpful if you specified what version wasn't working and then what version you downgraded to in order to get it working. – Ryan May 13 '21 at 18:35
1
.
.
{
    "parserOptions": {
    "ecmaVersion": 2020
},
.
.

Will do the trick.

Krishan Pal
  • 306
  • 1
  • 3
1

I had to update the ecmaVersion to "latest"

"parserOptions": {
    "parser": "@babel/eslint-parser",
    "sourceType": "module",
    "ecmaVersion": "latest",
    "ecmaFeatures": {
      "jsx": true,
      "experimentalObjectRestSpread": true
    },
    "requireConfigFile": false
  },
Philip Sopher
  • 627
  • 2
  • 8
  • 19
0

I'm using typescript and I solve this error change parser

....
"prettier/prettier": [
            "error",
            {
                .....
                "parser": "typescript",
                .....
            }
        ],
....
  • 1
    I'm unclear on where you're suggesting to make these changes. I'd appreciate you taking a look at my ESLint question here since you may be able to answer. Thanks! https://stackoverflow.com/q/67522592/470749 – Ryan May 13 '21 at 18:30
  • 2
    You must make these changes to the prettier / prettier rule in your eslint file – Dálcio Macuete Garcia May 18 '21 at 09:22