Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is ESLint integrated into Create React App?

When I run npx create-react-app ..., a bare-bone React project is being created for me. When I then peek into package.json, there seems to be some evidence of ESLint to be present, as there is this:

"eslintConfig": {   "extends": "react-app" }, 

However, whenever I install ESLint as a dev dependency and configure it -- as I usually do --, VS Code seems to pick it up. In this case, VS Code does not seem to recognize that there is any kind of linter present/configured. This is not super surprising, as ESLint is not a dependency of the React project I just generated -- at least not according to package.json. When I try to run eslint . within the project's root directory, it says "command not found".

I tried to breathe life into this ESLint configuration by expanding it, so now I have this:

"eslintConfig": {   "extends": ["react-app", "eslint:recommended", "google"],   "rules": {     "semi": ["error", "always"],     "quotes": ["error", "double"]    } }, 

This changes nothing. I manipulated the source code in a way that I know it violates the above configuration, yet, I have not been signaled any wrongdoing.

This leads me to a simple question: Do projects generated by create-react-app come with some kind of ESLint configuration, and, if so, how do I enable and extend it correctly?

As I am being referred to the number one Google hit that comes up when searching "create react app eslint" -- which I have obviously read --, let me clarify what I mean:

ESLint is obviously integrated into Create React App in a different way than it would be if it had been manually added to the project using like so. This is not only evident by the sheer number of people who post about their struggles of getting the two to work together. This is also evident as...

  • ...one cannot run the eslint command in the project root.
  • ...ESLint does not seem to be a dependency within package.json.
  • ...VS Code doesn't pick up that there is ESLint present.
  • ...there is no .eslintrc.* file in the project root.
  • ...etc.

So: How do I go about ESLint in the context of Create React App? For starters: How do I run it? How do I expand it? And why does VS Code not pick it up -- even though it usually notices the presence of ESLint?

like image 366
lkbaerenfaenger Avatar asked Jan 07 '20 17:01

lkbaerenfaenger


People also ask

Does create react app use ESLint?

Because Create React App comes with ESLint already integrated. They use their own sharable ESLint configuration and this can be found under the eslintConfig object in package.

Do I need to install ESLint With create react app?

Note, ESLint is installed with create-react-app, so you don't need to explicitly install it. Then install the packages for Airbnb config. This command will work for Yarn or NPM.

What ESLint config does create react app use?

The top node for ESLint in react-scripts is eslint-config-react-app . Some basic configuration is also part of the deal. So ESLint does work out of the box.


1 Answers

Yes, create-react-app comes with eslint config.

How do I enable and extend it correctly?

You can check how to extend it here.

{   "eslintConfig": {     "extends": ["react-app", "shared-config"],     "rules": {       "additional-rule": "warn"     },     "overrides": [       {         "files": ["**/*.ts?(x)"],         "rules": {           "additional-typescript-only-rule": "warn"         }       }     ]   } } 

How do I enable it?

You need to integrate it with your IDE.

How do I run it?

After integrating it, an eslint server will be running in the background and will enable linting for your IDE (sometimes restarting IDE required).


I checked all your claims after running npx create-react-app example:

...one cannot run the eslint command in the project root.

You can:

enter image description here

eslint is installed as part of the project dependency, just by running eslint globally (eslint [cmd]) you need to ensure it installed globally (not recommended).

...ESLint does not seem to be a dependency within package.json.

Why should it be? That's why you using a starter like CRA. It's an inner dependency, you don't need to worry about it, that's CRA's job.

...VS Code doesn't pick up that there is ESLint present.

It does, check the OUTPUT tab and look for ESLint to see the server's output.

enter image description here

...there is no .eslintrc.* file in the project root.

You get the default configuration from CRA (which is hidden from you for focusing on coding). Add such file if you want to override it (you can also extend it, check the docs).


Its very useful to understand what eslint actually is and how we use it React development, check out related question "Do React hooks really have to start with “use”?".

like image 80
Dennis Vash Avatar answered Sep 21 '22 10:09

Dennis Vash