Warning: React version not specified in 'eslint-plugin-react settings' while running eslint on top of React 18.2.0
Asked Answered
R

5

70

Issue

Git bash shows Warning: React version not specified in eslint-plugin-react settings. See https://github.com/jsx-eslint/eslint-plugin-react#configuration . while running eslint.

How to produce

create-react-app my-app
cd app
npm install eslint --save-dev
npx eslint --init
npx eslint .

package.json

{
  ...
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.4",
    "@testing-library/react": "^13.3.0",
    "@testing-library/user-event": "^13.5.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "5.0.1",
    "web-vitals": "^2.1.4"
  },
  ...
  "devDependencies": {
    "eslint": "^8.18.0",
    "eslint-plugin-react": "^7.30.1"
  }
}

I tried to find solutions but failed. I kindly ask for your help.

Reaction answered 28/6, 2022 at 3:0 Comment(0)
S
177

Add this to your config in .eslintrc:

{
  "settings": {
    "react": {
      "version": "detect"
    }
  }
}

See the config here: https://github.com/jsx-eslint/eslint-plugin-react/blob/master/README.md#configuration

Sanguinaria answered 17/7, 2022 at 20:1 Comment(5)
This should be the answer. Not sure why it's not marked checked.Cyzicus
Yeah, this actually works. I've accepted this answer, @BonAndreOpinaReaction
The settings property should be added to the eslintrc.json file along with the other options like rules and plugins.Bingham
@Bingham thanks, I wish the docs were more clear about that.Chaworth
I just wonder why this is not the default... You have to manually add a setting to detect automatically the versionRhombencephalon
D
11

Inside your folder root edit .eslintrc.js file and put "version": "detect". Like

module.exports = {
  settings: {
    react: {
     version: "detect",
    },
  },
 }
Devolve answered 16/3, 2023 at 13:40 Comment(0)
W
6

If you're looking at the new eslint.config.js, I did the below (with typescript). I tried to follow the official README but it wasn't too helpful.

import react from "eslint-plugin-react";
import typescriptEslint from "@typescript-eslint/eslint-plugin";
import typescriptParser from "@typescript-eslint/parser";
import globals from "globals";
import reactRecommended from "eslint-plugin-react/configs/recommended.js";

export default [
  {
    ignores: ["dist/**/*"],
  },
  {
    files: ["**/*.{js,jsx,mjs,cjs,ts,tsx}"],
    ignores: ["dist/**/*"],
    ...reactRecommended,
    settings: {
      version: "detect",
    },
    languageOptions: {
      ...reactRecommended.languageOptions,
      ecmaVersion: "latest",
      sourceType: "module",
      parser: typescriptParser,
      parserOptions: {
        ecmaFeatures: {
          jsx: true,
        },
      },
      globals: {
        ...globals.serviceworker,
        ...globals.browser,
      },
    },
    plugins: {
      "@typescript-eslint": typescriptEslint,
      react,
    },
    rules: {
      //rules here
    },
  },
];
Workable answered 28/10, 2023 at 15:52 Comment(2)
Just fyi, should be: settings: { react: { version: "detect" } } (but thanks for posting this because it helped!)Elda
Thanks gusterlover6 and @greg-jacobs Please consider updating your answer based on Greg's comment.Titleholder
K
0

add this in eslint.config.mjs before languageOptions

settings: {
      react: {
        version: "detect",
      },
},
languageOptions: {
      globals: globals.node,
},
Karena answered 18/7 at 21:31 Comment(0)
H
-9

The problem is in the eslint-plugin-react v.7.30.1. Downgrade it to v.7.30.0 and it will work

"eslint-plugin-react": "^7.30.0"
Hypoploid answered 7/7, 2022 at 11:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.