Catch Mistakes With ESLint

Your format-and-lint branch has Prettier now, and every file in the project looks tidy. That includes src/lib/closing-soon.js, the previous developer’s unfinished notice, which now has neat two-space indentation and double quotes. But a tidy file can still be a broken one, and Prettier wouldn’t tell you, because layout is all it looks at.

In this lesson, you’ll add ESLint to the same branch and tell it where each part of the project runs. You’ll read the problems it reports, fix them in the editor, and end with a branch that’s ready for its pull request.

Add ESLint to the Project

Some mistakes only show up when a line of code actually runs. Look at line 11 of closing-soon.js, where the previous developer meant to turn "21:00" into two numbers:

Language: JavaScript
  const [hour, minute] = today.close.split(":").map(Nubmer);

Nubmer is a typo for Number. JavaScript would only complain once the function reached that line, and the site doesn’t call this function yet, so the browser can’t show you the mistake. A linter reads your code without running it and reports likely bugs, such as a name that was never defined or a variable that’s never used. ESLint is the linter most JavaScript projects use.

You’ll use three packages to add ESLint, and like any new package, each one gets a quick check first.

Open the npm pages for eslint, @eslint/js, and globals, and check the name, the Weekly Downloads, the Repository, and the Last publish date. All three are downloaded millions of times a week and were published recently. The terminal confirms where each one comes from:

Language: Shell
$ npm view eslint repository.url
git+https://github.com/eslint/eslint.git
$ npm view @eslint/js repository.url
git+https://github.com/eslint/eslint.git
$ npm view globals repository.url
git+https://github.com/sindresorhus/globals.git

The first two come from ESLint’s own repository. The globals package lists the names that each environment, such as the browser or Node, provides, and you’ll see in a moment why ESLint needs it. Install all three as development dependencies, because you need them during development and the published pages never load them:

Language: Shell
$ npm install -D eslint @eslint/js globals

added 80 packages, and audited 98 packages in 2s

27 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

Unlike Prettier, these three keep npm’s usual caret range, because a newer linter release reports problems but doesn’t rewrite your files the way a newer formatter can. ESLint is installed now, but it doesn’t check anything until it knows which rules to apply and where your code runs.

Tell ESLint Where Your Code Runs

ESLint checks files without running them, so it can’t tell where each one will run. Your site’s code in src/ runs in the browser, which provides document. Line 9 of src/home.js uses it in const badge = document.querySelector("#open-badge");. Meanwhile, scripts/check.js, vite.config.js, and the eslint.config.js you’re about to write run in Node, which provides console and process.

Names like these, which an environment gives every file without an import, are globals. Unless ESLint knows each file’s globals, its no-undef rule reports every document and process the same way it reports Nubmer. The globals package holds the list of names for each environment.

ESLint also shouldn’t check two folders. dist/ is Vite’s build output, and src/vendor/ holds Generic Curbside’s widget. Since the widget is their code, you left it alone for Prettier too. ESLint reads all of this from a file named eslint.config.js in the project’s top folder. It exports an array of config objects, including the rule set that ESLint recommends. Create the file with this content:

Language: JavaScript Filename: eslint.config.js
import js from "@eslint/js";
import globals from "globals";

export default [
  // Vite's build output, and Generic Curbside's widget, which is their code.
  { ignores: ["dist/", "src/vendor/"] },
  // The recommended rules, for every file ESLint checks.
  js.configs.recommended,
  // The site's code runs in the browser.
  { files: ["src/**/*.js"], languageOptions: { globals: globals.browser } },
  // The check script and the config files run in Node, as ES modules.
  {
    files: ["scripts/**/*.js", "vite.config.js", "eslint.config.js"],
    languageOptions: { globals: globals.nodeBuiltin },
  },
];

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.