Set Up Eslint in node-js Application

what is Eslint:

ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, with the goal of making code more consistent and avoiding bugs.

Eslint analyzes your code and warns you of potential errors. In order for it to work, you need to configure it with specific rules.

why do we need eslint

Javascript is a language that doesn't have a specific way of writing it, which gives developers the liberty to write their javascript code the way they understand it and can read it better. Now imagine there are 10 developers working together on a javascript project. Of course, each developer has his/her way of writing his javascript code, so in the codebase, there will be readability issues because there would be 10 different patterns of code. This is where Eslint comes in. with some rules, eslint will flags errors when it detects a code pattern that is flaunting these rules

what are eslint rules

Rules are the style guide your code patterns have to follow. rules are manually set by developers. There is also so many style guide out there developed by different organizations and developers. we called them eslint-config. Eslint is there and set in a codebase to allow all developers to follow the same pattern of writing javascript code.

How do I set Eslint up in my node js project

There are different ways of doing this. You can choose to manually write your eslint rules yourself or use existing eslint-configs. we will be combining both in this article. The eslint-config that will be used is the air-BnB config as it is widely used among javascript developers now create a new folder and initialized a node project if you do not have an existing project, if you do, just change directory inside that folder and install the following dependencies as a dev-dependency.

eslint eslint-config-airbnb-base eslint-plugin-import

npm i -D eslint eslint-config-airbnb-base eslint-plugin-import

create a .eslintrc.js file at the root of your folder. Type this in the folder

module.exports = { 
      "extends": "airbnb-base"
 };

now download the eslint extension on your code editor(vs code) restart editor(vs code).

now let us add more rules to eslint.

module.exports = { 
      "extends": "airbnb-base",
       rules: {
 'comma-dangle': ['error', 'never'],
 'no-param-reassign': ['error', { props: false }],
 "quotes": ["error", "double"]
}
 };

To read on eslint and get to know more rules head on to https://eslint.org/ for more details.

references : https://eslint.org/docs/user-guide/getting-started