Contact information

1303, Shivalik Shilp, Iskcon Cross Rd, Ahmedabad 380015, INDIA

We are available 24/ 7. Call Now. +91 99741 86581 services@jarvisbitz.com

Introduction

Understanding ESLint concepts and rules are helpful for every Node.js project to reduce the amount of bulk in your work. Nicholas C. Zakas created the free, open-source ESLint JavaScript linting tool in June 2013. The most popular tool for linting Node.JS packages is ESLint. It is a pluggable and customizable linter tool for locating and reporting on JavaScript patterns. It analyses coding patterns using the AST (Abstract Syntax Tree).

Linting is the process of looking for both programmatic and stylistic faults in the source code. This is especially useful for figuring out some common and uncommon coding errors. I’ll demonstrate how to set up ESLint for a Node.js project in this article.

Why Formatting and Linting?

  • Find typos, syntax, and other errors
  • Observe good coding practices
  • Consistency of Code Style
  • Avoid writing bad code Caution: Do not use hazardous techniques

What does AirBnB have to do with all of this, you might be asking yourself at this point. Simply said, the Airbnb style guide is widely used because it is well-documented. If you look at their guide on GitHub, you’ll see that they provide explanations for everything as well as some excellent examples of both bad and good code.

How to configure ESLint

Install ESLint globally on your computer system before configuring it for your Node.js project:

Running ESLint init, which handles all the setup for us, is feasible after installing ESLint globally on your computer system.

I did a simple Node.js project. It is a basic node app that was started by:

npm init -y

We’ll proceed to install ESLint next.

eslint –init

Creates a.eslintrc file, the ESLint configuration file, in the current directory. This file is used by ESLint to choose which rules to use while analyzing your code. The files can be set up to be in CSS or JSON format. By typing ESLint -init, we may accomplish that.

When we run the code, it will prompt us with a series of questions, as seen in the image below. While you are free to choose what fits you, for this project, we will be employing a well-liked style guide.

Open the package.json file once the installation is complete. You’ll notice that we’ve now installed three new packages. This is how freshly installed devDependencies will appear:

“devDependencies”: {

“eslint”: “^8.0.1”,

“eslint-config-standard”: “^17.0.0”,

“eslint-plugin-import”: “^2.25.2”,

“eslint-plugin-node”: “^11.1.0”

}

The next step is to configure our ESLint files. ESLint is intended to be adjustable, so you can disable rules or add new ones. You can choose between two setup choices to make ESLint ideal for your project:

To specify specified information for each subdirectory and the main directory, the files configuration is configured using either JavaScript, JSON, or YAML files. In this project, JSON will be used.

After installation, you receive the.eslintrc.json file that makes ESLint run automatically. Using the command prompt, you may also choose your configuration file.

Setting global variables, configuring our environment, and then establishing rules are the next steps. Since the option will have complete influence over how ESLint handles your code, it is necessary.

Go to the.eslintrc.json file to configure the environment. An example of a JS object is as follows:

“scripts”: {

“start”: “nodemon server.js”,

“lint:check”: “eslint .”,

“lint:fix”: “eslint –fix .”

},

This should eliminate all contradictions in your code.

Let’s implement a few ES6 capabilities in our index.js code right away:

Open the index.js file and insert the following code:

Conclusion:

ESLint gives you the freedom to design your own rules or find the rules that are most relevant to your needs by developing your ability to write clear, concise, and effective code based on pre-existing or built-in rules. Your Node.JS development will stand out if you use ESLint in it.