One of the necessary steps in the growth of any software team is landing on a coding style. Every developer likes to write code a little differently. This is a good thing! Differences of opinion about how to write code allow teams to find novel solutions to problems they face. However, too much difference in terms of coding style can lead to difficulty working with a code base. In my experience, the healthiest software teams I’ve worked on settle on a list of rules for code that they enforce across the entire code base. Then, they take those rules a step further: they codify them into an automatic code-checking system. Called a “linter,” these are programs that examine your code and warn developers when parts of it don’t conform to the team’s rules.
When it comes to code linters, documentation is always one of the most important features. Both ESLint and JSHint are highly-configurable. You can tweak them to fit every nuance of your team’s coding style. But when you first dive in, all the rules are overwhelming. Do you want to turn on the eqeqeq rule? What does it even do? What about curly ?
This is where documentation is valuable. It’s also a place where my opinion is that JSHint has a real advantage. Both libraries have extensive documentation, but JSHint’s options reference is clear and concise. ESLint’s rules reference, by comparison, doesn’t explain nearly so clearly what each rule does. Instead, you need to click through on each rule explanation to read more about it. While both will explain to you how to use their system if you read them, JSHint’s documentation is simpler and more straightforward. One credit that I will give to ESLint, though, is that they provide example code
In addition to documentation, error messages are the other part of the most important parts of a linting library. Error messages are what tell you the mistakes you’ve made in writing your code. Clear error messages make working with a linting library a joy. They also simplify adding a new developer to the team. When that developer writes some code that doesn’t fit your team’s style, a good error message means it’s easy to understand where they went wrong, and what rule they broke. A poor error message causes confusion and frustration.
Error messages are a place where ESLint has a real advantage over JSHint. ESLint’s errors are succinct, but still clearly explain what they’re talking about. They can also provide a small snippet of the code with a carat to indicate where the error triggered during evaluation. ESLint’s swath of configuration options extend to error messages, too. You can define more than a dozen different output formats for ESLint errors. This makes it easy to use those error messages in whatever format you need.
By contrast, JSHint’s error messages aren’t nearly so versatile. JSHint will display errors, with a file name, line number and column number, as well as a short explanation of what’s wrong with the code. While this is useful, it doesn’t compare to the clarity provided by ESLint. The other major shortcoming for JSHint is that it doesn’t provide nearly as many output options. The only output choices are standard and verbose. The verbose output option provides more information. That information is useful, but it still doesn’t stand up to how useful ESLint’s error codes are.
Both ESLint and JSHint install via NPM and run on the command line. Examining them, JSHint isn’t as popular among developers or teams as ESLint. However, both are still actively developed and well-maintained. Installing each is as simple as running an npm install command. This is a place where neither has a clear advantage.
While installation is something that comes quickly and easily for both libraries, initial setup is a bit more involved. When it comes to JSHint, you need to define a .jshintrc file in the root of your project. That file contains instructions for JSHint to use while linting your code. Your reference for those rules is the options reference we looked at earlier in the post. Setting up this file can be pretty time-consuming. In my experience, it’s often the place where the most pain happens while integrating a linter into your code base. Code that was perfectly fine yesterday is now throwing an error today. That’s not the kind of change that developers enjoy!
ESLint also requires that you set up a configuration file. However, ESLint comes packaged with an initialization script that simplifies this process. After you install ESLint, you should run npx eslint --init . This command will walk you through a series of options to help you set ESLint up for the first time. It provides an easy way to adopt rule sets used by large engineering teams, or you can walk through a list of rules, see their explanations, and choose to turn them on or off one-by-one. There’s even an option to have ESLint examine your existing code base, then set rules based off the patterns it sees in your code. This avoids the frustration you see in the initial setup process. Code never goes from valid to invalid. Instead, you can slowly tweak rules as the need arises, while ensuring new code conforms to the patterns of old code.
So, Which Should Your Team Choose?
With all of that said, I don’t feel like JSHint is a bad choice. If you’re a team that’s already happy with JSHint, should you switch to ESLint? Probably not! ESLint isn’t sufficiently more advanced to throw away the hours of work you’ve spent configuring JSHint just how you like it. And while it’s easier to onboard a new developer to ESLint, the learning curve for JSHint obviously isn’t so steep that it’s impossible to learn. Both are powerful libraries, and while I feel that ESLint provides more advantages, any mature software team would be well-served by either.
This post was written by Eric Boersma. Eric is a software developer and development manager who’s done everything from IT security in pharmaceuticals to writing intelligence software for the US government to building international development teams for non-profits. He loves to talk about the things he’s learned along the way, and he enjoys listening to and learning from others as well.