So far, you should have a working JavaScript with Webpack application. In this tutorial, we will take this one step further by introducing ESLint for an enforced unified code style without code smells. Code style becomes an important topic for developers. If you just code for yourself, it might be alright to violate best practices. However, in a team of developers you have to have a common code style as foundation. You should follow the same rules to make your code look alike. It helps others developers to read your code, but also to avoid code smells.
ESLint in JavaScript helps you to set up rules and to enforce code style across your code base. Letâs get started by installing the eslint library (node package). You can install it in your project from your projectâs root directory on the command line:
You may also want to install the ESLint extension/plugin for your editor/IDE. For instance, in VSCode you can find the ESLint extension on their marketplace. Afterward, you should see all the ESLint errors in your editorâs/IDEâs output.
Since the project uses Webpack, you have to tell Webpack that you want to use eslint in your build process. Therefore you can install eslint-webpack-plugin on the command line to your projectâs dependencies from your projectâs root folder:
Next, you can use the new Webpack plugin for ESLint in your Webpack webpack.config.js file:
Now, all source code that goes through Weback will be checked by ESLint automatically. Once you start your application, it will output an error though: âNo ESLint configuration foundâ. You need this file to define your ESLint configuration. Create it in your projectâs root directory on the command line:
Then, create an empty ESLint rule set in this new .eslintrc file:
Later on you can specify rules in this file. But first, letâs try to start your app again. You might run (again) into Parsing errors such as âThe keyword âimportâ is reservedâ or âThe keyword âexportâ is reservedâ. The error happens, because ESLint does not know about Babel enabled JavaScript features yet. For instance, the import or export statements are JavaScript ES6 features. Therefore, you have to use babel-eslint node package to lint source code that is valid Babel interpreted JavaScript. From your projectâs root directory type:
Then, in your .eslintrc configuration file, add @babel/eslint-parser as parser:
Note: If the previous error regarding Babel enabled JavaScript features still shows up in your IDE/editor â because you may have installed an ESLint plugin/extension, restart your IDE/editor and check whether the error still shows up. It shouldnât.
You should be able to start your application without any ESLint errors now. There are no errors displayed, because you didnât specify any rules yet.
ESLint rules apply for a lot of different code style use cases. Check out the list of available ESLint rules yourself. For the sake of learning about ESLint rules, letâs add our first rule in the .eslintrc configuration file for ESLint:
The rule checks the length of characters in a line of code. If the length is more than 70 characters, you will get a warning once you start your application with npm start or in your IDE/editor in case a plugin or extension for ESLint. Try to call up this warning by writing a line of code longer than 70 characters. ESLint should tell you something like: âThis line has a length of <XX> . Maximum allowed is 70â. You can adjust the rule to allow some more characters:
If you still see warnings, it is your first chance to improve the code style in your codebase.
Now, it would be very tidious to come up with a set of ESLint rules for every JavaScript project. Thatâs why itâs possible to share them as libraries (node packages). There are various shareable ESLint configs out there, however, one of the most popular one is the Airbnb ESLint configuration based on Airbnbâs Style Guide . You can install the configuration in addition to all its peer dependencies with the following command on the command line from your projectâs root directory:
Afterward, you can introduce it in your .eslintrc configuration file for ESLint:
Note: Itâs up to you to keep your own ESLint rules (e.g. max-len from before) to extend the ESLint rule set from Airbnb. However, my recommendation would not be to come with your own ESLint rules. Instead, pick one of the more popular ESLint configuration by large companies and follow their guidance. If you are already advanced in JavaScript, you (and your team) can start to add your own flavor to the ESLint rules by extending it or by coming up with a configuration entirely on your own.
After starting your application on the command line again or checking the output in your IDE/editor with an installed ESLint plugin/extension, you may see new ESLint warnings/errors popping up. Thatâs a good point in time to start fixing them.
Sometimes you might see a lot of ESLint rule violations on your command line or in your IDE/editor. Often it is up to you to fix them to follow the common best practices. However, whenever you are unsure about the ESLint warning, search it in your favorite search engine and evaluate whether you want to have this ESLint rule or not. You can either fix the warning in the mentioned source code file or remove/disable the rule altogether, if you think you donât need it.
In case you want to remove a ESLint rule globally, just remove it from your .eslintrc file in case you specified it yourself and it doesnât come from any popular style guide (e.g. Airbnb). If the latter is the case, you can only disable the rule. For instance, the no-unused-vars ESLint rule from Airbnbâs ESLint configuration could be disable the following way:
However, you can also disable your own or extended ESLint rules in the respective source code file:
Also you can disable an ESLint rule in the whole or rest of a file by not enabling the ESLint rule again:
Now, you should have all the ESLint knowledge at your hands to have a unified code style with best practices by using a popular ESLint configuration such as the one from Airbnb. You also know how to add your own rules, how to show violations in your IDE/editor/command line, how to fix violations, and how to remove/disable ESLint rules.
The tutorial has shown you how to install ESLint on a per project basis with npm install --save-dev eslint . Also you stepped through the whole process of setting up the ESLint configuration and installing a shareable ESLint configuration yourself. However, there is an more effortless way of doing it in the end. You can install ESLint globally to make it kinda accessible for all of your JavaScript projects with npm install -g eslint .
Still, once your JavaScript project is set up, you need to run eslint --init in the root directory of your project on the command line which will install a local copy of ESLint for your project again. Also you will see a command line prompt that you can step through to set up your ESLint configuration dynamically. In the end, thatâs my recommended way of setting up ESLint for your JavaScript project.