ESLint is a pretty cool tool that cleans up your Javascript code for you! But when you're deving on an IDE such as IntelliJ/WebStorm/VSCode, who wants to be running npm run lint
all the time am I right?
"Would be pretty awesome to have my IDE run my linting automatically... But how?" - You.
You've come to the right place my friend! 🎉
IntellJ/WebStorm
Let's start with IntelliJ first because it's the easiest! It's usually already set up on IntelliJ (and subsequently WebStorm). In case it isn't, here are the steps to enable it.
I'm assuming you already have a .eslintrc.*
file in your project root directory. This is usually standard practice. If not, make one and put all your settings in there!
For context, the project I'll be using in this example is a Vue project.
-
Find your Settings (where this is differs for Windows/OSX)
-
Go to Language & Frameworks > Javascript > Code Quality Tools > ESLint
-
Make sure that the "Automatic ESLint configuration" is checked! Like the screenshot says, it will use the ESLint package as well as the
.eslintrc.*
to run the linter. Now, in the files that you work on, the IDE will pick up any mistakes you made and highlight them for you to fix up! Easy!
VSCode
VSCode is a little trickier to set up as it doesn't come with a tool to run ESLint out of a box. You have to install it as an extension.
The most popular ESLint extention by far is ESLint by Dirk Baeumer.
Once it is installed, make sure it is enabled (by clicking on the Enable button if it isn't already), and just like the setup for IntelliJ, check that your .eslintrc.*
file is in your project root!
-
Open the Command Palette
-
Search for this Command: "ESLint: Manage Library Execution"
-
A popup like below should show up asking if you want to allow the ESLint extension to run the ESLint you've installed in your project using the
.eslintrc.*
config file. This is different to clicking the 'Enable' button after you install the ESLint Extension so make sure you don't skip this step! As for which option you should pick.Allow Everywhere
enables ESLint for all workspaces (or projects) which is useful if all your project happen to be Javascript based. OtherwiseAllow
will just enable it for this project and you have to repeat these steps for all subsequent projects. -
Now just open up your Javascript files and start fixing up any mistakes the linter picks up!
Linting .vue
files!
If you're linting .vue
files like I am, the ESLint extension doesn't pick up Vue SFC files. If your project is pure Javascript or React, you don't need to worry about this extra step!
According to the official eslint-plugin-vue guide here, we need to create a .vscode
folder in the root directory of the project and within it, create a settings.json
file.
Inside that file, you'll need to add a bit of extra config for the ESLint plugin to pick up .vue
files.
{
"eslint.validate": [
"javascript",
"javascriptreact",
"vue"
],
"vetur.validation.template": false
}
The vetur
setting is if you have the vetur plugin installed.
And that's it! Happy Linting!
Top comments (0)