186

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  @typescript-eslint/no-use-before-define

Code in my component starts with:

import React from "react";

Eslint settings:

module.exports = {
  parser: "@typescript-eslint/parser",
  parserOptions: {
    ecmaVersion: 2020,
    sourceType: "module",
    ecmaFeatures: {
      jsx: true
    }
  },
  settings: {
    react: {
      version: "detect"
    }
  },
  extends: [
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended",
    "prettier/@typescript-eslint",
    "plugin:prettier/recommended"
  ],
  rules: {
    "@typescript-eslint/explicit-module-boundary-types": 0,
    "@typescript-eslint/triple-slash-reference": 0,
    "no-use-before-define": "off",
    "@typescript-eslint/no-use-before-define": "off"
  },
};

Some part of package.json:

"devDependencies": {
  "@typescript-eslint/eslint-plugin": "^4.1.0",
  "@typescript-eslint/parser": "^4.1.0",
  "babel-eslint": "^10.1.0",
  "eslint": "^6.6.0",
  "eslint-config-airbnb": "^18.1.0",
  "eslint-config-prettier": "^6.11.0",
  "eslint-plugin-import": "^2.20.2",
  "eslint-plugin-prettier": "^3.1.3",
  "eslint-plugin-react": "^7.20.0",
  "prettier": "^2.0.5",
  "start-server-and-test": "^1.11.3"
},
"dependencies": {
  ...
  "react-scripts": "3.4.3",
  ...
}

I tried:

Alexey Nazarov
  • 2,289
  • 2
  • 12
  • 13

17 Answers17

319

From the official documentation.

"rules": {
  // note you must disable the base rule as it can report incorrect errors
  "no-use-before-define": "off",
  "@typescript-eslint/no-use-before-define": ["error"]
}
igor
  • 5,351
  • 2
  • 26
  • 22
  • 7
    See also this recommendation from a maintainer of typescript-eslint https://github.com/typescript-eslint/typescript-eslint/issues/2540#issuecomment-692505191 – jtschoonhoven Oct 27 '20 at 21:09
  • 2
    Doesn't work. I am using `@typescript-eslint` 4.22. – codenamezero Apr 16 '21 at 15:09
  • The rules settings as mentioned above, worked for me. Turn off the first rule which is buggy and turn on the second which is works fine. – Praym Dec 23 '21 at 22:58
  • 1
    Adding just `"no-use-before-define": "off",` to my `.eslintrc.json` rules did it for me. – Ian Dec 30 '21 at 05:03
  • @Praym first rule is not buggy. *Original* ESLint rules are valid for JS only even though many work with TS with no issues. You should switch off JS rule before using a **matching** TS rule. https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/README.md#supported-rules – igor May 02 '22 at 17:05
115

The bug occurs due to mismatch of @typescript-eslint versions in react-scripts and your local package.json - GitHub issue

You can downgrade the packages until react-scripts updates their version.

    "@typescript-eslint/eslint-plugin": "4.0.1",
    "@typescript-eslint/parser": "4.0.1",

EDIT 2020-09-14

It seems the bug is not related to react-scripts version of @typescript-eslint since multiple people reported the same bug without using react-scripts.

Anyway, the workaround remains the same - downgrade to the working version of @typescript-eslint until the fix is available.

EDIT 2020-10-24

react-scripts@4.0.0 has been published with updated @typescript-eslint. Using the newest version should solve the issue.

EDIT 2020-11-04

If after upgrading the packages the error is still there, most probably your eslint config uses the wrong rule. Check out Igor's answer to fix it.

Eric Majerus
  • 1,099
  • 1
  • 12
  • 23
sashko
  • 1,632
  • 1
  • 13
  • 20
  • 4
    did not worked for me. "react": "^16.13.1", "typescript": "~3.7.2", "@typescript-eslint/eslint-plugin": "^4.0.1", "@typescript-eslint/parser": "^4.0.1". Same error as the post. – Rolly Sep 23 '20 at 15:23
  • 2
    Have you tried deleting `node_modules` and reinstalling the packages? – sashko Sep 23 '20 at 16:03
  • i did but the warning keeps there. – Rolly Sep 24 '20 at 15:28
  • 3
    In your `package.json` specify `typescript-eslint` version without caret `^` as it will install the latest minor version, not `4.0.1`. – sashko Sep 24 '20 at 16:55
  • Upgrading `react-scripts` to v4.0.0 (`next` as of this writing) fixes this. `npm i react-scripts@next`. – pho3nixf1re Sep 29 '20 at 17:41
  • Can confirm that using react-scripts@next and the latest typescript-eslint deps resolves the issue – Ben Gotow Sep 30 '20 at 13:08
  • It worked for me only after downgrading to the versions specified ( without caret obviously) – Rahul Gandhi Nov 02 '20 at 16:45
  • check "@typescript-eslint/eslint-plugin" at yarn.lock or package-lock.json, if other lib uses other version, sync your version – Sacru2red Jun 30 '22 at 02:08
14

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"
  ],
}
Raine Revere
  • 30,985
  • 5
  • 40
  • 52
  • I had to put this in overrides: "parser": "@typescript-eslint/parser", "plugins": ["@typescript-eslint/eslint-plugin"], – Little Brain Apr 06 '21 at 16:30
10

That is how I resolved my problem.

  1. First of all, go to the node_modules/react-scripts/config/webpack.config.js and change cache to false, because this will help you to understand what works and what is not then you changing eslint file. I found it here
 cache: true, // You can set it to false
 formatter: require.resolve('react-dev-utils/eslintFormatter'),
  1. Add ENV varialbe to .env file. More info
 EXTEND_ESLINT=true
  1. From now CRA will have to use your local eslint for linting during your build and we have control to disable some rules, in my case in .eslintrc:
rules: {
    "@typescript-eslint/no-use-before-define": "off"
},
Alexey Nazarov
  • 2,289
  • 2
  • 12
  • 13
  • Careful on modifying it directly as it'd get overwritten by a yarn/npm install. I'd recommend to do the webpack.config.js modification through craco: https://github.com/gsoft-inc/craco – Jose A Oct 20 '20 at 12:30
7

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"
}
j0hnm4r5
  • 407
  • 3
  • 17
  • This saved me thanks. After doing some digging it seems like gatsby installs both of those libs with ^2.24.0 version which is super old and they get used for some reason. So frustrating that on the docs it says, just make your own .eslintrc and all good, and then they include their own parser and ruleset that gets used. – Domagoj Vuković Jan 29 '21 at 23:33
5

I got this error from the airbnb-base ruleset in a typescript project. Also extending airbnb-typescript (that has the correct rules for typescript) resolved the issue.

thisismydesign
  • 21,553
  • 9
  • 123
  • 126
4

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

Bnaya
  • 765
  • 6
  • 15
  • for me, it wasn't just react-scripts that kept an outdated version of `@typescript-eslint` as a dependency. i found out the other culprits with `yarn list @typescript-eslint/eslint-plugin @typescript-eslint/parser` – feihcsim Oct 19 '20 at 06:10
3

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. Ran 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.

maxshuty
  • 9,708
  • 13
  • 64
  • 77
St John
  • 431
  • 4
  • 5
3

This has been resolved in 4.4.0. Upgrade these dependencies to version 4.4.0

Update: This may work for some use cases. This resolved my issue.

"@typescript-eslint/eslint-plugin": "^4.4.0",
"@typescript-eslint/parser": "^4.4.0",
   
etoxin
  • 4,908
  • 3
  • 38
  • 50
2

Try adding import/resolver to your Eslint settings:

  "settings": {
    "react": { "version": "detect" },
    "import/resolver": { "typescript": {} }
  }

Maybe you will need to install it too:

$ yarn add -D eslint-import-resolver-typescript

If that doesn't work, you could change this rule

"@typescript-eslint/no-use-before-define": "off"

like this:

"no-use-before-define": "off"
1

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],

Jean Poffo
  • 19
  • 4
1

If you use Create React App. Getting the latest version of react-scripts fixed this issue for me. Currently: 4.0.3

// Get this specific version:
npm uninstall react-scripts
npm install react-scripts@4.0.3

// Get latest version:
npm uninstall react-scripts
npm install react-scripts
MapMyMind
  • 113
  • 10
0

(not enough rep to comment)

@sashko's workaround seems to work - deleting node_modules was necessary as well.

One thing I missed was the caret in "^4.0.1". The version needs to be fixed without the caret ^, which seems to be the issue that @Rolly is having. Make sure it's 4.0.1.

Only a workaround until react-scripts updates to v4 (I'm using create-react-app)

jmaio
  • 98
  • 1
  • 5
0

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

0

I deleted packages @typescript-eslint/eslint-plugin and @typescript-eslint/parser from my package.json, deleted node_modules and package-lock.json, reinstalled packages: npm install The error disappeared.

UPDATE

After that create-react-app uses their own @typescript-eslint/eslint-plugin module which is deprecated.

Eugene
  • 45
  • 6
0

This Answer worked for me in a react-native project(airbnb style guide)

  1. Extend your rules with airbnb-typescript inside the .eslintrc.json
"extends": {
  "airbnb-typescript"
}
  1. Press ctrl+shift+p or cmd+shift+p >Reload window (or simply close and re-open vscode)
Nivethan
  • 2,339
  • 19
  • 22
-3

Try using Yarn instead of NPM (make sure to remove your node_modules in between).

This worked for me.

Antoine
  • 5,504
  • 5
  • 33
  • 54