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
83.2k19 gold badges163 silver badges170 bronze badges
asked Mar 15, 2016 at 2:19
2
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.
answered Apr 15, 2017 at 12:56
hanorinehanorine
6,9263 gold badges13 silver badges18 bronze badges
11
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
},
answered Sep 26, 2019 at 19:59
Alvin KondaAlvin Konda
2,66821 silver badges22 bronze badges
3
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.
answered Mar 15, 2016 at 6:19
Kevan AhlquistKevan Ahlquist
5,2151 gold badge17 silver badges23 bronze badges
6
"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
answered Jul 25, 2017 at 9:56
7
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"
}
answered Apr 4, 2019 at 4:04
JoeeJoee
1,75416 silver badges18 bronze badges
1
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
}
}
answered Mar 16, 2020 at 16:12
Vojtech RuzickaVojtech Ruzicka
15.9k15 gold badges63 silver badges64 bronze badges
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,
},
};
answered Sep 23, 2021 at 6:49
sh_arksh_ark
5475 silver badges14 bronze badges
1
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
answered Feb 5, 2020 at 23:07
CristianoCristiano
1342 silver badges4 bronze badges
I solved this problem by setting this in .eslintrc.json file:
"extends": [
...,
"plugin:prettier/recommended"
]
Dharman♦
29.3k21 gold badges80 silver badges131 bronze badges
answered Sep 1, 2021 at 8:55
PazuPazu
1312 silver badges7 bronze badges
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
answered Mar 3, 2021 at 17:18
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"],
},
};
answered May 14, 2022 at 6:37
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:
- I deleted
$HOME/.nvm
folder which was not deleted when I removednvm
earlier. - Verified node is the latest and did proper parser options.
- It started working!
answered Oct 8, 2019 at 11:47
Asim K TAsim K T
16.2k10 gold badges76 silver badges98 bronze badges
I was facing the issue despite implementing all the above solutions. When I downgraded the eslint version, it started working
answered May 5, 2021 at 18:46
1
I’m using typescript and I solve this error change
parser
....
"prettier/prettier": [
"error",
{
.....
"parser": "typescript",
.....
}
],
....
answered Apr 28, 2021 at 9:36
2
.
.
{
"parserOptions": {
"ecmaVersion": 2020
},
.
.
Will do the trick.
answered Nov 14, 2022 at 7:09
I had to update the ecmaVersion
to "latest"
"parserOptions": {
"parser": "@babel/eslint-parser",
"sourceType": "module",
"ecmaVersion": "latest",
"ecmaFeatures": {
"jsx": true,
"experimentalObjectRestSpread": true
},
"requireConfigFile": false
},
answered Nov 28, 2022 at 20:39
Philip SopherPhilip Sopher
4872 gold badges5 silver badges18 bronze badges
Tell us about your environment
- ESLint Version: 4.10.0
- Node Version: 8.10.0
- npm Version: 5.6.0
What parser (default, Babel-ESLint, etc.) are you using? babel-eslint
Please show your full configuration:
Configuration
{ "extends": [ "react-app", "plugin:flowtype/recommended", "plugin:jest/recommended", "prettier" ], "parser": "babel-eslint", "plugins": ["flowtype", "jest", "prettier"], "env": { "browser": true }, "overrides": [ { "files": ["src/**/*.spec.js"], "env": { "jest": true } } ], "rules": { "comma-dangle": ["error", "never"], "no-underscore-dangle": ["error", { "allowAfterThis": true }], "max-len": ["error", { "ignoreTrailingComments": true, "code": 100 }], "react/jsx-filename-extension": 0, "react/sort-comp": [ "error", { "order": [ "type-annotations", "state", "defaultProps", "static-methods", "lifecycle", "everything-else", "render" ] } ], "react/require-default-props": "off", "react/jsx-wrap-multilines": "off", "react/jsx-indent": "off", "react/jsx-indent-props": "off", "react/jsx-closing-bracket-location": "off", "class-methods-use-this": "off", "no-global-assign": 2, "no-param-reassign": "off", "no-plusplus": "off", "no-console": "error", "prettier/prettier": "error", "camelcase": [1, { "properties": "always" }], "no-unused-vars": [2, { "args": "after-used", "ignoreRestSiblings": true }] } }
What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.
import React from "react"; const ChatWidget = () => { return ( <script> window.fcWidget.init({ token: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", host: "https://wchat.freshchat.com" }); </script> ); }; export default ChatWidget;
which runs eslint src --max-warnings=0
What did you expect to happen?
I expected eslint to not flag anything.
What actually happened? Please include the actual, raw output from ESLint.
yarn run v1.7.0 $ eslint src --max-warnings=0 /Users/justinbrown/Sync/code/iu-react/src/components/ChatWidget/index.js 7:14 error Parsing error: Unexpected token, expected } 5 | <script> 6 | window.fcWidget.init({ > 7 | token: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", | ^ 8 | host: "https://wchat.freshchat.com" 9 | }); 10 | </script> ✖ 1 problem (1 error, 0 warnings) error Command failed with exit code 1.
Notes
I’ve looked through a number of Stack posts as well as issues posted on this repo. I’ve tried setting ecmaVersion explicitly as well as other suggestions as proposed in posts like: https://stackoverflow.com/questions/37918769/eslint-parsing-error-unexpected-token and #10369
Содержание
- How To Fix – “Parsing error: Unexpected Token” in React.js or Javascript ?
- How To Fix – “Parsing error: Unexpected Token” in React.js or Javascript ?
- Primitive Check:
- Parsing error: Unexpected token, expected > #10628
- Comments
How To Fix – “Parsing error: Unexpected Token” in React.js or Javascript ?
How To Fix – “Parsing error: Unexpected Token” in React.js or Javascript ?
In this post, we will explore – How To Fix – “Parsing Error: Unexpected Token” in React.
Such “Unexpected token” signify a common behavioural problem – somewhere in your code a specific construct was supplied, but the system expected something completely different (based on defined rulescustoms). This could be due to a typo mistake, missing something in the code etc. Also could be dev environment and ESLint parsing styles are not compatible owing to the changes for various or upgraded versions.
Before we try to fix, Let’s do some primitive checks to pinpoint some of the symptoms.
Primitive Check:
- What version of React are you using ?
- Have you considered and cross-checked that you have correctly “imported” everything ?
- Are you able to compile your code using Babel ?
- Have you tried babel-eslint parser – the newer name is @babel/eslint-parser
- Does it work in the Browser ?
Once you have been through all the above pointers, try the below options.
Try the latest babel eslint and make sure you follow the compatibility matrix.
ESLint | babel-eslint |
---|---|
4.x | >= 6.x |
3.x | >= 6.x |
2.x | >= 6.x |
1.x | >= 5.x |
Node version preferably should be 8+.
- Do the install. The “x.x” & “y” below has to be substituted appropriately as per the above table.
- Go to the .eslintrc (or .eslintrc.json) config file and make the below changes.
Also check what value you have in “.babelrc”.
If above doesn’t work, you could also try,
- If applicable, go to your package.json file and check the below.
- You might have two files in your system. There are different versions of ecmaScript with the same name,
- .eslintrc.js -> es6: true – which was released on June 2015
- .eslintrc.json -> “ecmaVersion”: 2017 – which is equivalent to es8
If the wrong ecmaScript is being picked up in your case, you can try removing the “.eslintrc.js” file (so as to avoid the conflict) and see if that helps to solve the error.
- You can also try changing the ecmaVersion in the .eslintrc.json file.
You could also try other values like 2017, 2018 instead of 2020
You can use one of the below ecmaVersion value to specify what version of ECMAScript syntax to use-
Источник
Parsing error: Unexpected token, expected > #10628
Tell us about your environment
- ESLint Version: 4.10.0
- Node Version: 8.10.0
- npm Version: 5.6.0
What parser (default, Babel-ESLint, etc.) are you using? babel-eslint
Please show your full configuration:
What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.
which runs eslint src —max-warnings=0
What did you expect to happen?
I expected eslint to not flag anything.
What actually happened? Please include the actual, raw output from ESLint.
Notes
I’ve looked through a number of Stack posts as well as issues posted on this repo. I’ve tried setting ecmaVersion explicitly as well as other suggestions as proposed in posts like: https://stackoverflow.com/questions/37918769/eslint-parsing-error-unexpected-token and #10369
The text was updated successfully, but these errors were encountered:
Hi, thanks for creating an issue.
This is happening because your syntax is invalid. It looks like you’re trying to use an inline tag as a JSX element, but tags in JSX don’t work quite the same way that they do in HTML (for example, still have to follow JSX syntax, so they can’t contain arbitrary JavaScript code).
As an immediate fix for your problem to get your code to parse, if you just want that code to run whenever your component is rendered, you could omit the tag and simply do something like this:
As a more long-term fix, it generally isn’t good practice for React components to have side-effects when rendering. Instead, you could initialize the widget in a componentDidMount method, with something like this:
Источник
Hi there!
I’m Arisa, a freelance Full Stack Developer.
I’m developing Lilac, an online school with hands-on Frontend e-books and tutoring👩💻
What is this article about?
This is a solution when I saw these errors.
Parsing error: unexpected token =>
Enter fullscreen mode
Exit fullscreen mode
Warning: React version not specified in eslint-plugin-react settings. See https://github.com/yannickcr/eslint-plugin-react#configuration .
Enter fullscreen mode
Exit fullscreen mode
Project environment & full configuration
- macOS: Catalina 10.15.7
-
VS Code
-
ESLint Version: ^7.15.0
-
Node Version: v12.18.2
-
npm Version: 6.14.5
"babel-eslint": "^10.1.0",
"babel-jest": "^22.4.1",
"babel-preset-env": "^1.6.1",
"concurrently": "^3.6.0",
"eslint": "^7.15.0",
"eslint-plugin-react": "^7.22.0",
"jest": "^22.4.2",
"webpack": "^3.10.0",
"webpack-dev-middleware": "^2.0.4",
"webpack-dev-server": "^2.11.1",
"webpack-hot-middleware": "^2.21.0"
Enter fullscreen mode
Exit fullscreen mode
A very simple project with JS(EcmaScript2016) with webpack, babel, jest and ESLint.
When did this error appear?
While I was running ESLint on my JS files.
How did it solve?
Reason
Lacking out of a package, babel-eslint
to install.
Solution steps
- Install
babel-eslint
locally
$ yarn add --dev babel-eslint
Enter fullscreen mode
Exit fullscreen mode
Source: https://github.com/babel/babel-eslint
- Add
"parser"
config in .eslintrc.js
"parser": "babel-eslint"
Enter fullscreen mode
Exit fullscreen mode
Source: https://github.com/eslint/eslint/issues/10137
- Remove unnecessary config for non-React project
// "extends": [
// "eslint:recommended",
// "plugin:react/recommended"
// ],
Enter fullscreen mode
Exit fullscreen mode
Source: https://github.com/yannickcr/eslint-plugin-react#configuration
No errors✨
Summary
The biggest mistake I had was the React config in a JS project.
Normally, I use React and didn’t realize the wrong config for JS project🤦♀️
Hope this article was relevant for what you were looking for!
Happy new year & happy coding🎍
Hey 😍
Want to help the DEV Community feel more like a community?
Head over to the Welcome Thread and greet some new community members!
It only takes a minute of your time, and goes a long way!