DEV Community

Damola Adekoya
Damola Adekoya

Posted on • Edited on

Eslint Vscode Setting up ESLINT in your JavaScript Project with VS Code

ESLINT: have you ever wonder what ESLINT is all about, when i first heard of ESLINT i was curious what it actually all about, ever since then i have been using it in my project, although at first i was using it wrongly, that is why I am putting up this post to allow people to get it right. but before i dive in let me quickly explain what is ESLINT and VS Code.

ESLINT is pluggable linting utility for Javascript and JSX, it help to discover possible errors.

VS Code is one of the top editor for development it was developed and maintain by Microsoft it help to improve productivity and also comes with many features, one of the feature i am going to emphasize on is extension. Extension are external packages in VS Code that allows you to extend the functionalities of your editor

you can download VS Code from their official website VS Code Download

NB: I won't be digging deep into VS Code..everything on VS Code in this post will only be related to ESLINT.

steps:

  • create a javascript project
  • install eslint as an extension in your VS Code Editor
  • Install eslint as a global package using npm
  • initialize eslint in your javascript project
  • modify your eslint configuration file in your project.

let create a simple javascript project using

npm init --yes

alt text

after the operation was successful it will create a package.json file that will manage all configuration for our project.

let try to install ESLINT extension on vs code editor

alt text

once we have installed eslint extension on our vs code editor then let install eslint as a global package via npm using the below code

npm install eslint -g

Enter fullscreen mode Exit fullscreen mode

you need to initialize eslint in your project so you can leverage the power of eslint. From your root project enter the below code to initialize eslint

eslint --init

Enter fullscreen mode Exit fullscreen mode

during the initialization eslint will ask you some questions, more like to setup your configuration file.

  • How would you like to use ESLint?

    • To check syntax only => it helps you correct your syntax and make sure it conform to standard.
    • To check syntax and find problems => to help you check for syntax correctness and also help to find any problems in your code base
    • To check syntax, find problems, and enforce code style_ => to help you check for syntax, find problem and enforce style, enforcing style means to conforms to a particular coding standard such as Airbnb, Google and other Standard coding style. But I always go for the last option the one with syntax, find problems and enforce code style
  • What type of modules does your project use?

    • Javascript module (import/export) => if your project has babel installed then you definitely need to choose this option. If you are working on a project such as React, Vue, Angular e.t.c they all use babel so you need choose this option.
    • CommonJS (require/exports) => this option is meant for commonJS that has nothing to do with babel, maybe your nodejs project and any other javascript project
  • Which framework does your project use?

    • React => if you are using react in/for your project then this option is for you
    • Vue => if you are using Vue in/for your project then this option is for you
    • None of these => if you are using neither React or Vue in your project choose this option
  • Where does your code run?

    • Browser => if your project runs on browser e.g React, Angular, Vue e.t.c then go for this option
    • Node => if your project is a node based then gladly choose this option
  • How would you like to define a style for your project?

    • Use a popular style guide => This allows you to choose from set of popular style such as Airbnb,Standard and Google style guide, it is advisable to choose this option in order for you to follow popular and most used style guide and i will be choosen this option in this post.
    • Answer questions about your style: This is for custom style guide
    • Inspect your JavaScript file(s).: custom style guide
  • What format do you want your config file to be in?

    • Javascript => whether you want your eslint config file to be in .js file
    • YAML => whether you want your eslint config file to be in .yaml file
    • JSON => whether you want your eslint config file to be in .json file you can choose any option in this section

after you have chosen your preferred configuration file type it will then prompt you to install all necessary dependencies. after all neccessary dependencies has been successfully installed it will now generate a config file with ".eslintrc"."js/json/yaml".

example of the the configuration file shown below
alt text

below is a little animated image that show how vs code works with eslint to notify you of errors in your javascript project

alt text

Setting up rules for ESLINT in your project

Defining rules for ESLINT in your project informed eslint the kind of rules you want to add or remove. you can modify/set your rules in the rules section in the configuration file

example of rules to set are

"rules" : {
  no-console: 0; 
  no-empty: 0;
  no-irregular-whitespace:0;

}

Enter fullscreen mode Exit fullscreen mode

you can define as many rules as possible, you can read more on ESLINT rules on their official documentation ESLINT Rules Documentation

Lastly, I am going to show you how you can link your eslint to your javascript project compiler/transpiler

steps below

  • Goto your package.json file, in the script segment in your file add the following
script:{
    "lint":"eslint"

}

Enter fullscreen mode Exit fullscreen mode

NB: "lint" is just an ordinary word, you can use any word you are comfortable with

then in your root project you can run your linting script with

npm run lint

Enter fullscreen mode Exit fullscreen mode

ESLINT helps to increase productivity, write your code according to standard and flag errors when your code base is breaking style guide rules. With this post you should be able to integrate ESLINT in your Javascript project.

Top comments (10)

Collapse
 
thedavyloper profile image
David A. Akpan

Thanks was really helpful, been trying to configure eslint to work without success until i read your article, i added lint as a script exactly as in your tutorial but it didn't work, i did "lint": "eslint myfile.js" instead and it worked.
thanks a lot.

Collapse
 
devdammak profile image
Damola Adekoya

I'm glad the article was able to help....
Using "lint: eslint myfile.js" will only lint a single file which is myfile.js.

But using "lint: eslint" will work for the entire project.

Collapse
 
gethubt profile image
GetHubT

This article was extremely helpful, and gave an immediate fix/solution to a consistent problem I had. Thank you for providing your expertise.

Collapse
 
devdammak profile image
Damola Adekoya

I'm glad the article was able to help you..

Collapse
 
keyjoshua profile image
key joshua

Quick Guide Bruh

Collapse
 
devdammak profile image
Damola Adekoya

I'm glad you like it

Collapse
 
nirnejak profile image
Jitendra Nirnejak • Edited

Nice article, recently I wrote an article about the same covering pre-commit as well inkoop.io/blog/setup-eslint-for-re...

Collapse
 
irina_kats profile image
Irina Kats

Thanks for the instructions, but is it a part of the best practices to install EsLint globally with npm install eslint -g?

Collapse
 
smithniels profile image
Niels Smith

Great article, thanks! :)

Collapse
 
jayesht profile image
Jay

Nice article. Thanks for sharing.