Setup ESLint In VSCode With `create-react-app` Without Ejecting
react
create-react-app
vscode
eslint
nodejs
In this post, I explain how I got to setup ESLint with create-react-app
(CRA) in Visual Studio Code, without ejecting!
Typically, we are asked to eject our CRA application when wanting to get advantage of ESLint integration with VS Code. However, here are some very easy steps I used for an easy fix while:
- Making code linting work in VSCode, AND
- Not breaking CRA setup (no eject)
Steps
-
Make sure you have installed the
ESLint
plugin for VS Code by Dirk Baeumer. However, DO NOT install the npmeslint
, whether locally or globally! The installation of the plugins says you should npm installeslint
but since CRA already provides its own, we skip that altogether. -
Make sure you configure your VSCode User and Workspace Settings with the settings you like for your ESLint User Config in the VS Code settings. I personally like to have
Auto Fix On Save
switched on, but you can personalize as you like. -
Add your
.eslintrc.json
to the project root, or runnpx eslint --init
to create one. If you create one manually, then jump to step 5 from here. (Note: We usenpx
here so that we simply execute--init
without installing theeslint
dependency. See step 1: Not installingeslint
) -
The
npx
command will create theeslintrc
file. If it asks Would you like to install them now with npm?, make sure to replyn
. We want to make sure we do not installeslint
. However, go ahead an install all other dependencies other thaneslint
. You need to install any required additional dependencies if they were not already done. The example below shows the case for the React plugin, needed to support React linting.
yarn add -D eslint-plugin-react
- Finally, make sure to have
"plugin:react/recommended"
setup ineslintrc
. Replaceextends
with an array if needed. The order here is important to make the correct linting work.
"extends": [
"eslint:recommended",
"plugin:react/recommended"
]
Here is a full rules list example that I use on my project.
{
"env": {
"browser": true,
"commonjs": true,
"es6": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended"
],
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": ["react"],
"rules": {
"indent": ["error", 2],
"linebreak-style": ["error", "unix"],
"quotes": ["error", "single"],
"semi": ["error", "always"]
}
}
That’s it! Now, refresh your files or re-open your project folder if needed and checkout those squigly ESLint familiar underline. And if you switched on Auto Fix On Save
in your VS Code User Settings, then CTRL+S or CMD+S will do all apply all the fixes for you.
Summary
Basically, as long as you don’t npm install eslint
itself, you are good. The conflict only happens when you npm install eslint
because CRA already has a specific version that it needs.
Happy coding!
Versions Used
- Node:
10.15.1
- Yarn:
1.13.0
- NPM:
6.8.0
- create-react-app: Always
latest
when usingnpx