ESLint

ESLint is a tool for analyzing your JavaScript code and making sure it follows certain rules, rules that you can customize.

Want to make sure every instruction has a semi-colon at the end of it? Want to make sure you only ever use double quotes for strings? ESLint has you covered.

Like most npm modules it's pretty easy to install:

npm install -g eslint  

We're using a -g flag here so ESLint is installed globally.

Next you'll want to set the initial config for your project. Go to whatever project you want to us ESLint with, and in the root directory, type and run:

eslint --init  

Now you're ready to go! Just run the eslint on any file you want:

eslint some_file.js  

Or you can run eslint on multiple files. Here's how I'm running it for a current project:

eslint *.js front-end/js/*.js  

If your file violates some rule, ESLint will tell you about it. Some of the rule violations can even be fixed by ESLint itself by using the --fix flag:

eslint --fix *.js front-end/js/*.js  

If you want to customize your rules, have a look at the .eslintrc.json file that should now be in your root directory (assuming you chose JSON as your file format, should look similar in any event). Mine looks like this:

{
    "env": {
        "browser": true,
        "node": true,
        "commonjs": true,
        "es6": true
    },
    "extends": "eslint:recommended",
    "installedESLint": true,
    "parserOptions": {
        "ecmaFeatures": {
            "experimentalObjectRestSpread": true
        }
    },
    "rules": {
        "indent": [
            "error",
            4
        ],
        "linebreak-style": [
            "error",
            "unix"
        ],
        "quotes": [
            "error",
            "double"
        ],
        "semi": [
            "error",
            "never"
        ],
        "no-console": 0
    }
}

env is the spot to tell ESLint what environment you're running your code in. For simplicity's sake I just told it all files could either be running on Node or in the browser.

I'll admit I don't yet know what all of these options are, but I think the most interesting part of this file is the rules section. You can find a whole list of rules at eslint.org/docs/rules.

Each rule's page shows you its configuration. If you want to turn a rule off that is (by default) on, just list it in the rules section with a value of 0. I did this with the no-console rule.

There are also a ton of plugins you can use with ESLint. Just search for "eslintplugin" or "eslint" on npmjs.com.

You can read more about ESLint on eslint.org and its npm listing.

Looking for a software developer?