Why should you use ESLint?
ESLint is a tool called a Linter. Linters are static code analysis tools that review your code against recommended rules and best practices in order to identify potential infractions. This tends to lead to more consistent code in both readability and style that can lead to fewer runtime errors due to certain things being caught ahead of time (e.g. attempting to reassign a constant). If you are on a team of developers, ESLint helps build that consistency even further as everyone will be working with the same recommendations and validating their work against such.
A quick mention about text editors
Installing NodeJS and NPM
Once NodeJS and NPM is installed, open up a Terminal (Mac) or Command Prompt (PC) window. You can verify the installed version with by typing
node --version and/or
package.json file in order to keep track of our dependencies for NPM. This can be accomplished by simply typing
npm init -y. (NOTE: This post won’t get into details of NPM or the
package.json file. I’ll dive into them in greater detail in a future post.)
To install ESLint, we need to install its package globally via NPM. To do this, simply type the command
npm install -g eslint. For Mac and Linux users, you may receive an error stating you don’t have permissions, if this is the case type
sudo in front of the command and enter your password when prompted. Once the package is installed, we now have to configure ESLint. Thankfully ESLint comes with its on utility for doing this by typing in
During the initialization and configuration process you’ll be asked a series of questions. The first of which configures how strict you want ESLint to be with the default set to checking syntax and finding problems (as shown above).
After all of those questions, it’ll prompt you to install some additional NPM packages based on your answers and will create a
.eslintrc configuration file.
Configuring VS Code
ESLint by Dirk Baeumer. Simply install the extension and restart VS Code and we are ready to have ESLint work with us while we type our code.
And That’s It!
function is declared but never used, when to use
let/const instead of
var, and a large number of other items. To learn more about ESLint and the rules that it can be configured to use, I highly recommend checking out the official website at https://eslint.org/. In a future post, we’ll dive deeper into ESLint when using it in conjunction with unit testing and with Typescript.