简体   繁体   English

JSX 中的 eslint“解析错误:意外标记 {”

[英]eslint "parsing error: Unexpected token {" in JSX

const title = 'My Minimal React Webpack Babel Setups';

const App = () => (<div><b>{title}</b><img src={img} /></div>)

This code occurs an error "ESLint Parsing Error: Unexpected token {"此代码出现错误“ESLint Parsing Error: Unexpected token {”

my .eslintrc.js file is like that我的.eslintrc.js文件就是这样

module.exports = {
    "extends": "airbnb"
};

and I install the packages like that然后我像那样安装软件包

"eslint": "^5.9.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-loader": "^2.1.1",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsx-a11y": "^6.1.2",
"eslint-plugin-react": "^7.11.1",

I thought that ESLint can read JSX because the token "<" doesn't occur error.我认为 ESLint 可以读取 JSX,因为令牌“<”不会发生错误。 (When I change the extends section in.eslintrc.js file to "airbnb-base", It occurs error "ESLint Parsing Error: Unexpected token <. But now, the token "<" doesn't occur error) (当我将.eslintrc.js 文件中的扩展部分更改为“airbnb-base”时,出现错误“ESLint Parsing Error: Unexpected token <。但是现在,令牌“<”不会发生错误)

However, my ESLint cannot read the JSX syntax line {variable}但是,我的 ESLint 无法读取 JSX 语法行 {variable}

Eslint on its own is not good enough. Eslint本身就不够好。 First install babel-eslint : 首先安装babel-eslint

npm install --save-dev babel-eslint

Or with yarn: 或者用纱线:

yarn add -D babel-eslint

Then add to your .eslintrc file: 然后添加到.eslintrc文件:

"parser": "babel-eslint"

You might want to install eslint-plugin-babel as well, but I believe this is not needed 您可能也想安装eslint-plugin-babel ,但我相信这不是必需的

I've got the same error on Next.js . 我在Next.js上遇到了同样的错误。

These steps solved my issue: 这些步骤解决了我的问题:

1) Install babel-eslint : 1)安装babel-eslint

npm install --save-dev babel-eslint

2) Add babel-eslint as a parser to eslint config 2)将babel-eslint添加为eslint配置的解析器

"parser": "babel-eslint"

My eslint config looks like this ( .eslintrc ): 我的eslint配置看起来像这样( .eslintrc ):

{
  "env": {
    "browser": true,
    "es6": true,
    "commonjs": true,
    "node": true
  },
  "extends": ["eslint:recommended", "plugin:react/recommended"],
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaVersion": 9,
    "ecmaFeatures": {
      "jsx": true
    },
    "sourceType": "module"
  },
  "plugins": ["react"],
  "rules": {
    "react/react-in-jsx-scope": 0,
    "no-console": 1
  }
}

For all of those coming to this post as of March 2020 or later, there have been some updates to babel and eslint that result in the accepted answer being out of date.对于所有在 2020 年 3 月或之后来到这篇文章的人来说, babeleslint已经进行了一些更新,导致已接受的答案已过时。

Basically, if you're writing React and not using a framework like Next.js or create-react-app, and you're needing to configure eslint to work appropriately by hand, here is a quick guide to follow.基本上,如果您正在编写 React 而不是使用 Next.js 或 create-react-app 之类的框架,并且您需要手动配置eslint以使其正常工作,请遵循以下快速指南。

Assumptions假设

This is assuming that you're starting a new project as of March 2020 and working with eslint 8.8 or later这是假设您从 2020 年 3 月开始一个新项目并使用eslint 8.8 或更高版本

What to Install安装什么

Run the following运行以下

npm install @babel/eslint-parser @babel/preset-react

If you don't have eslint-plugin-react installed, you'll want to install that too in order to utilize the recommended eslint settings for React projects.如果您没有安装eslint-plugin-react ,您也需要安装它以便为 React 项目使用推荐的eslint设置。

Important : If you have babel-eslint installed or present in your package.json still, npm uninstall it.重要提示:如果您的package.json中仍然安装或存在babel-eslint ,请将其npm uninstall

How to Configure It如何配置

Example .eslintrc.js file (or .eslintrc or .eslintrc.json )示例.eslintrc.js文件(或.eslintrc.eslintrc.json

module.exports = {
  env: {
    browser: true,
    es2021: true,
  },
  extends: ['eslint:recommended', 'plugin:react/recommended'],
  parser: '@babel/eslint-parser', // <<<< Important
  parserOptions: {
    requireConfigFile: false, // <<<< Allows you to skip Eslint complaining that you don't have a .babelrc file 
    babelOptions: {
      presets: ['@babel/preset-react'], // <<<< Important
    },
    ecmaFeatures: {
      jsx: true, // <<<< Important
    },
    ecmaVersion: 'latest',
    sourceType: 'module',
  },
  rules: {},
};

After following these updates, my project started working as expected.遵循这些更新后,我的项目开始按预期工作。

References参考

My .eslintr has this extra configuration to enable JSX 我的.eslintr有这个额外的配置来启用JSX

"parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM