Tutorial

How To Lint and Format Code with ESLint in Visual Studio Code

VS Code

Introduction

When writing JavaScript with an editor such as Visual Studio Code, there are a number of ways you can ensure your code is syntactically correct and in line with current best practices. You can use a linter to do this. Linters check your code for syntax errors and highlight errors to make sure you can quickly find and fix them. ESLint is a linter that you can integrate into your Visual Studio Code setup in order to ensure code integrity.

ESLint can both format your code and analyze it to make suggestions for improvement. It is also configurable. This means that you can customize how your code is evaluated.

In this tutorial, you will set up ESLint on Visual Studio Code and implement a custom configuration to deal with log statements in debugging. You will also configure ESLint to automatically fix syntax errors when you save your files.

Prerequisites

To complete this tutorial, you will need the following:

Step 1 — Creating JavaScript Starter Code

You need to start with a demo project. Create a directory for your project with the following command:

  • mkdir linting

Now that your project folder is created switch into the linting directory:

cd linting

While inside of the linting directory, create a JavaScript file with the name app.js:

  • touch app.js

Open app.js in Visual Studio Code. Write the following JavaScript code in your app.js file:

linting/app.js
const name = 'James'

const person = {first: name}

console.log(person)

const sayHelloLinting = (fName) => {
console.log(`Hello linting, ${fName}`);
};

From a formatting perspective, you may notice several things that could be improved:

  • Inconsistent use of quotes
  • Inconsistent use of semicolons
  • Spacing

With this JavaScript file in place, you can now initialize this project. To do this, navigate back to your command line and in the linting directory, run the following command:

  • npm init

Using the npm init command to initialize your project will create a package.json file in the linting directory. The package.json will store your project dependencies and other important configuration settings for your project.

Output after running npm init

Now that your JavaScript project is properly set up, you can now set up ESLint.

Step 2 — Setting Up ESLint

Before you set up ESLint for your project, you will first need to install ESLint:

npm install eslint --save-dev

It’s important to include the --save-dev flag because this saves the package as a dependency for development usage only. In this case, eslint is a package that is only needed when you are actively working on and making changes to your project. Once your project launches or is in production, eslint will no longer be needed. Using the --save-dev flag ensures that eslint will be listed in your package.json file as a development dependency only.

Now that ESLint is installed, you can initialize an ESLint configuration for your project using the following command:

  • ./node_modules/.bin/eslint --init

An important piece in this command is the --init flag. The ./node_modules/.bin/eslint section of the command is the path to ESLint in your project. Using the --init flag activates ESLint for your project. Activating or initializing ESLint will create an ESLint configuration file that will allow you to customize how ESLint works with your project.

Before you can access your ESLint configuration file, you will be prompted with different questions about your project. These questions are asked to make sure that the configuration that is initialized for your project best fits your needs.

The first prompt will be:

? How would you like to use ESLint? …
  To check syntax only
  To check syntax and find problems
❯ To check syntax, find problems, and enforce code style

Choose the To check syntax, find problems, and enforce code style option.

The next prompt will be:

 What type of modules does your project use? …
  JavaScript modules (import/export)
❯ CommonJS (require/exports)
  None of these

Choose the CommonJS option to use CommonJS global variables.

The next prompt will say:

? Which framework does your project use? …
  React
  Vue.js
❯ None of these

Choose the None of these option.

The next prompt will ask:

? Does your project use TypeScript? › No / Yes

Choose the No option.

The following prompt will say:

? Where does your code run? …  (Press <space> to select, <a> to toggle all, <i> to invert selection)
✔ Browser
✔ Node

Choose the Browser option.

The next prompt will say:

✔ How would you like to define a style for your project? …
❯ Use a popular style guide
  Answer questions about your style
  Inspect your JavaScript file(s)

Choose the Use a popular style guide option.

For the Which style guide do you want to follow? prompt, choose the Airbnb: https://github.com/airbnb/javascript option.

The next prompt will ask:

? What format do you want your config file to be in? …
  JavaScript
  YAML
❯ JSON

Choose the JSON option.

You will then see this message:

Checking peerDependencies of eslint-config-airbnb-base@latest
The config that you've selected requires the following dependencies:

eslint-config-airbnb-base@latest eslint@^5.16.0 || ^6.8.0 || ^7.2.0 eslint-plugin-import@^2.21.2

The last prompt will ask:

? Would you like to install them now with npm? › No / Yes

Choose the Yes option to install the dependencies with npm.

You will also be asked to install extra packages. Choose yes.

After completing all the prompts, you’ll notice that a file named .eslintrc.json has been added to your linting directory. ESLint is now installed. The code in app.js hasn’t changed yet. This is because ESLint needs to be integrated with Visual Studio Code.

Step 3 — Configuring ESLint

To integrate ESLint into Visual Studio Code, you will need to install the ESLint extension for Visual Studio Code. Navigate back to Visual Studio Code and search for ESLint in the Extensions tab. Click Install once you have located the extension:

ESLint extension in Visual Studio Code

Once ESLint is installed in Visual Studio Code, you’ll notice colorful underlining in your app.js file highlighting errors. These markers are color-coded based on severity. If you hover over your underlined code, you will see a message that explains the error to you. In this way, ESLint helps us find and remove code and syntax errors.

ESLint can do even more for you. ESLint can be modified to automatically fix errors every time a file is saved.

Step 4 — Formatting on Save

To configure ESLint to automatically fix syntax and formatting issues every time you save, you will need to open the settings menu. To find the settings in Visual Studio Code, click on the gear icon in the lower left, and then choose Settings.

Within the settings menu, search for Code Actions on Save. The first option will say Editor: Code Actions on Save and below that, there will be an option to Edit in settings.json. Click the link to Edit in settings.json.

The settings.json file will open inside of your code editor. For ESLint to fix errors when you save your file, you will need to write the following code in settings.json:

settings.json
"editor.codeActionsOnSave": {
  "source.fixAll.eslint": true
},
"eslint.validate": ["javascript"]

With this code in your settings.json file, ESLint will now automatically correct errors and validate JavaScript on save.

Return back to your app.js file and save it. You will see some changes, including less colorful underlining. Some of the formatting issues that ESLint has fixed include:

  • Consistent use of single quotes
  • Proper indentation inside of the function
  • Consistent use of semicolons

JavaScript code in Visual Studio Code

ESLint will now automatically solve syntax errors whenever you save app.js. There are still some remaining error messages. These can be fixed by customizing the ESLint configuration to catch or ignore specific errors and formatting issues.

Step 5 — Customizing ESLint Configuration

As is, ESLint produces a highlighted message for all console.log() statements in app.js. In some cases, removing console.log statements may not be a priority. You can customize the ESLint configuration to allow console.log statements without producing an error message. ESLint configuration rules can be modified in the .eslintrc.json file.

Open up the .eslintrc.json file. This is the code you will see in that file:

linting/.eslintrc.json
{
    "env": {
        "browser": true,
        "commonjs": true,
        "es2021": true
    },
    "extends": [
        "airbnb-base"
    ],
    "parserOptions": {
        "ecmaVersion": 12
    },
    "rules": {
    }
}

At the bottom of the .eslintrc.json file, you will see a "rules" object. To customize the errors that trigger ESLint or to disable ESLint’s response to certain pieces of code, you will add key-value pairs to the "rules" object. The key will match the name of the rule you want to add or change. The value will match the severity level of the issue. You have three choices for severity level:

  • error - produces a red underline
  • warn - will produce a yellow underline
  • off - will not display anything.

If you do not want to produce any error messages for console.log statements, you can use the no-console rule name as the key. Input off as the value for no-console:

linting/.eslintrc.json
"rules" : {
  "no-console": "off"
}

This removes the error messages from your console.log statements in app.js:

Error messages removed

Some rules require multiple pieces of information, including a severity level and a value. To specify the type of quotes you want to use in your code, you have to pass in both the chosen type of quotes and the severity level:

linting/.eslintrc.json
"rules" : {
  "no-console": "off",
   "quotes": [
      "error",
      "double"
    ]
}

Now, if you include single quotes in your quote, ESLint will raise an error.

Conclusion

This tutorial introduces some of what you can do with linting using ESLint on Visual Studio Code. Linting tools like ESLint can help create time for more complex tasks by automating and simplifying how you verify syntax and best practices.

If you would like more information about rules and what key-value pairs you can use to customize your ESLint rules, you can check out this documentation.

Creative Commons License