'React' was used before it was defined

前端 未结 14 2005
傲寒
傲寒 2020-12-29 19:28

I am working with create-react-app + typescript + eslint application and during build have such error:

Line 1:8:  \'React\' was used before it was defined  @t         


        
相关标签:
14条回答
  • 2020-12-29 19:59

    If you are only getting this error for .js files, make sure @typescript-eslint/parser is being used exclusively on Typescript files.

    .eslintrc.json (abbreviated)

    {
      "overrides": [
        {
          "files": ["**/*.ts", "**/*.tsx"],
          "plugins": ["@typescript-eslint"],
          "rules": {
            "no-use-before-define": "off",
            "@typescript-eslint/no-use-before-define": ["error"],
          },
        }
      ],
      // WRONG: Do not use @typescript-eslint/parser on JS files
      // "parser": "@typescript-eslint/parser",
      "plugins": [
        "react",
        // "@typescript-eslint"
      ],
    }
    
    0 讨论(0)
  • 2020-12-29 20:03

    My fix for that: Use eslint 6 as react-scripts is using Force react scripts to use newer @typescript-eslint packages as the rest of the repo. I'm using selective resolution here

      "resolutions": {
        "**/react-scripts/**/@typescript-eslint/eslint-plugin": "^4.4.1",
        "**/react-scripts/**/@typescript-eslint/parser": "^4.4.1"
      }
    

    @typescript-eslint 4.x does still support es6

    0 讨论(0)
  • 2020-12-29 20:03

    Not enough credit to comment.

    @sashko thank you for your answers.

    I just wanted to add that for me, I did the following.

    1. Removed all eslint related dependencies from package.json if they were already included in package-lock.json by react-scripts (for me that was all of them)
    2. Deleted my node_modules folder, and package-lock.json file
    3. npm install

    After completing those steps I finally got that error to go away. I prefer removing the dependencies to downgrading them since this will help avoid the same issue happening again in the future.

    0 讨论(0)
  • 2020-12-29 20:04

    It is an issue present in ESLINT.

    The way I resolve is to add the following lines to the ESLINT/rules:

    "no-use-before-define": [0],
    "@ typescript-eslint / no-use-before-define": [1],

    0 讨论(0)
  • 2020-12-29 20:04

    deleting the node_modules folder and reinstalling it worked for me. I'm using yarn as package manager.

    0 讨论(0)
  • 2020-12-29 20:07

    I landed on this page after I started getting issues with a Gatsby project that was using Typescript and an ESLint config that extended eslint-config-airbnb-typescript. In addition to OP's error, ESLint was also giving errors on various rules not being defined, like Definition for rule '@typescript-eslint/no-redeclare' was not found. and a few others.

    The underlying problem ended up being that Gatsby was using fairly old versions of both @typescript-eslint/eslint-plugin and @typescript-eslint/parser. Forcing yarn to install only up-to-date versions solved the issue:

    // package.json
    "resolutions": {
        "@typescript-eslint/eslint-plugin": "^4.4.0",
        "@typescript-eslint/parser": "^4.4.0"
    }
    
    0 讨论(0)
提交回复
热议问题