How to configure .eslintrc config file with example in NodeJS
In this article, you are going to learn about how to configure the .eslintrc config file with an example in NodeJS.
Eslint is basically a program that constantly scans code and finds out potential coding errors or bad coding practices. It is very easy to configure and also used for formatting code. To configure eslint, you need to follow a few steps. First, you need to install the eslint extension from VSCode. All you need to do is to go to the extension option and search for eslint and you will get the extension like the following:
After installing it. you need to download some dev dependencies to make things workable. To do so you need to open your terminal and set the project path where you want to configure eslint and give the following command in the terminal.
We have installed these dev dependencies and you need to download these locally for every project if you want to enable eslint. If you download these globally then things will not work.
Finally, you need to configure your eslint and set up coding rules into .eslintrc.json file. Here, in our case, we have set some common rules in our .eslintrc.json file. See the below:
{
"extends": ["airbnb", "prettier", "plugin:node/recommended"],
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "off",
"spaced-comment": "off",
"no-console": "warn",
"consistent-return": "off",
"func-names": "off",
"object-shorthand": "off",
"no-process-exit": "off",
"no-param-reassign": "off",
"no-return-await": "off",
"no-underscore-dangle": "off",
"class-methods-use-this": "off",
"prefer-destructuring": ["error", { "object": true, "array": false }],
"no-unused-vars": ["error"]
}
}
Now let’s see a few examples that our eslint shows error or not. In your case, if it is not working, all you need to do is to close the vs code project and open it again.
You can see that we are getting an error from eslint where we have declared a variable but never used it. Let’s see another example if we try to reassign a constant value.
You can see that in this time we are getting 2 errors from eslint where it is being said that we can not reassign constant variable. These rules are being set by us in .eslintrc.json file.
This is how you may configure .eslintrc config file in NodeJS. Moreover, you may visit the official eslint site and configure your file with the rules that you want to apply.